我正在使用wpf和多绑定到一个ViewModel列表。
假设我有一个ViewModel类型相同的ObservableCollection,如下所示:
代码:
public class ShapeVM
{
public Color Color { get; set; }
public string Name { get; set; }
}
ObservableCollection ShapeVMs = new ObservableCollection();
ShapeVMs.Add(...);
ShapeVMs.Add(...);
ShapeVMs.Add(...);
ShapeVMs.Add(...);
ShapeVMs.Add(...);
// There are 5 ShapeVM in the collection.查看:
<UserControl .........>
<ColorBox SelectedColor="{Binding Path=Color, Mode=TwoWay}" />
</UserControl>有没有可能每当ColorBox的SelectedColor发生变化时,5个ShapeVM的颜色会同时自动更改为ColorBox的SelectedColor?
如果我将UserControl的DataContext设置为任一ShapeVM,则只会更改任一ShapeVM的颜色。
但是,我想在ColorBox的SelectedColor更改的同时更改5个ShapeVM。我怎么能这样做呢?
非常感谢。
发布于 2012-07-12 11:56:09
您问题的另一个简单解决方案是
<ComboBox x:Name="cmb" Grid.Row="0" ItemsSource="{Binding ShapeVMs}" DisplayMemberPath="Color" Height="40" SelectedValue="{Binding Path=SelectedColor,ElementName=clrbox}" SelectedValuePath="Color"/>
<ColorBox x:Name="clrbox"/>我希望这篇文章能有所帮助。
发布于 2012-07-12 16:50:12
将VM的颜色属性绑定到ColorBox:
从DependencyObject
绑定绑定=新建绑定();inding.source = TheColorBox;binding.Path =新建绑定(ColorBox.SelectedColorProperty);shapeVM.SetBinding(ShapeVM.ColorProperty,绑定);
(由于某些原因,代码标记不起作用,抱歉)
https://stackoverflow.com/questions/11444089
复制相似问题