【发布时间】:2014-02-12 16:00:51
【问题描述】:
案例是:我有一个控件的事件,我希望我的 ViewModel 对其做出反应。目前我正在通过执行如下示例中的不可见按钮命令来执行此操作。
在 View.xaml 中:
<Control x:Name="SearchResultGrid" ... DataRefreshed="SearchResultRefreshed" />
<Button x:Name="SearchResultRefreshedButton" Visibility="Collapsed" Command="{Binding SearchResultRefreshedCommand}" />
在 View.xaml.cs 中:
private void SearchResultRefreshed(object sender, EventArgs e)
{
if (SearchResultRefreshedButton.Command != null)
{
SearchResultRefreshedButton.Command.Execute(SearchResultGrid.ResultRowCount);
}
}
这很好用,但对我来说似乎是一个 hack。我想知道是否有更好的(标准)方法来做到这一点?我找不到任何例子,这就是我自己“发明”的。
【问题讨论】:
-
Google 的交互触发器。
-
或者只是将视图的 DataContext 转换为 ViewModel 并做任何你想做的事情。ViewModel vm = this.DataContext as ViewModel;那么你可以做 vm.SomeAction
-
@RohitVats 谢谢,我在一些例子中看到了它们的使用,但它看起来不像我需要的。现在我发现我错了。