【发布时间】:2011-11-15 06:02:55
【问题描述】:
我正在使用 WPF 并使用数据绑定。
我想做一个 UserControl,它有一个可以用于数据绑定的属性。
另外,如果属性发生变化,我想更新 UserControl 中的一些其他属性。
例如,
public class MyControl : UserControl
{
....
....
....
....
public ViewStyles CurrentView
{
get { return (ViewStyles)GetValue(CurrentViewProperty); }
set
{
SetValue(CurrentViewProperty, value);
UpdateView();
}
}
public static readonly DependencyProperty CurrentViewProperty = DependencyProperty.Register("CurrentView", typeof(ViewStyles), typeof(ComboView));
....
.....
.....
.....
}
问题来了:
使用了一个ViewModel,其中有一个属性ViewStyle,绑定到上面的CurrentView。
另一个控件组合框也与 ViewModel 中的 ViewStyle 数据绑定。
实际上,我想使用组合框来选择控件的不同视图。如何在 MVVM 中实现?
我试过上面的方法。但是,UI(MyControl 的不同 ViewStyles)没有改变。只有当我用鼠标点击它时它才会改变。
谢谢。
XAML:(我的控件)
<Views:MyControl Grid.Column="1" Grid.Row="1" Height="505" HorizontalAlignment="Left" Margin="2,0,0,0" Name="comboView1" VerticalAlignment="Top" Width="983"
ViewStyle="{Binding Path=CurrentView}" BorderThickness="5" BorderBrush="Black" ItemsSource="{Binding Path=Images}"
SelectedIndex="{Binding Path=CurrentIndex}" Foreground="White"
</Views:MyControl>
XAML:(组合框)
<ComboBox Margin="0,3,1,0" Width="178" HorizontalAlignment="Right" Name="ViewDDBox" FontSize="13" Foreground="#FFF6F3F3" Background="#FF444444"
BorderThickness="2" Height="23" VerticalAlignment="Top" Grid.Column="1"
ItemsSource="{Binding Path=ViewTypes}" IsEnabled="True" SelectedValue="{Binding Path=CurrentView, Mode=TwoWay}">
</ComboBox>
假设在Combobox中选择后MyControl的视图(一些UI效果)会发生变化。但是现在,只有当我使用鼠标单击 MyControl 时它才会改变。
【问题讨论】:
-
能否发一些数据绑定的xaml代码?
标签: c# wpf data-binding mvvm