【发布时间】:2020-05-02 10:39:43
【问题描述】:
我有以下 xaml:
<DataGridTemplateColumn Header="123">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Role}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.Roles,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path = DataContext.UpdateUserCommand}" /> // bind fails there
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
问题是我的命令没有在更改时运行,但是如果将组合框定义移出数据网格,我的命令将成功触发。好像我的绑定错了,但我不知道是什么问题。
【问题讨论】:
-
为什么不直接将 ComboBox 的 SelectedItem 属性绑定到视图模型属性?
-
我需要对此事件进行异步处理,对我而言,绑定到事件是唯一可能的解决方案。更改所选项目后,我想通过我的服务启动异步数据库更新
-
您可以在视图模型中注册一个异步 PropertyChanged 事件处理程序,以对其自身的属性更改做出反应...
-
@Clemens 宁愿修改 PropertyChanged 事件引发器。绑定到您自己的事件没有多大意义
-
@Clemens 这似乎是一个更清洁的解决方案,但我没有找到任何这样做的例子,我没有太多处理事件的经验,所以我不确定如何实施这个