我想用Propertychanged.Fody重构我的代码,如本页所示 http://www.mutzl.com/tag/mvvm-light/
普通代码:
private string _platformSelectedItem;
public string PlatformSelectedItem
{
get { return _platformSelectedItem; }
set
{
if (_platformSelectedItem == value) return;
_platformSelectedItem = value;
// Perform any pre-notification process here.
GetData();
RaisePropertyChanged();
}
}
自
public string PlatformSelectedItem {get; private set}
该属性绑定到一个组合框,并且组合框的值是基于另一个组合框的动态的,因此我有我的方法 GetData()。
<ComboBox ItemsSource="{Binding Platforms}" SelectedItem="{Binding PlatformSelectedItem, Mode=TwoWay}" Grid.Column="1" Grid.Row="2" Height="20" Grid.ColumnSpan="2" Margin="0,3,15.667,3"/>
如果我将代码重构为自动属性,则必须通过单击/打开组合框来执行该方法。
我应该使用带有命令的事件触发器是一种更简单的方法吗?
基于线程,我们可以使用<i:Interaction。触发器>在 WPF MVVM 中(不在 Silverlight 中)
我的最终解决方案看起来:
视图模型:
属性区域:
public RelayCommand SelectionChangedCommand { get; private set; }
构造 函数:
SelectionChangedCommand = new RelayCommand(Update);
方法领域:
private void Update()
{
GetData();
}
然后在我的 UI 中:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<ComboBox ItemsSource="{Binding Platforms}" SelectedItem="{Binding PlatformSelectedItem, Mode=TwoWay}" Grid.Column="1" Grid.Row="2" Height="20" Grid.ColumnSpan="2" Margin="0,3,15.667,3">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>