我正在用 MVVM 风格的 WPF 构建一个应用程序。我正在尝试在我的 DataGrid
上制作过滤器当我选中或取消选中几个复选框进行过滤时。
我找到了 Interaction.Triggers
的解决方案,但在这种情况下它对我不起作用。
这是我的代码:
<ListBox
ItemsSource="{Binding PortsFilterSource}"
Background="LightGray"
BorderThickness="0"
Grid.Column="1">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox
Content="{Binding Name}"
IsChecked="{Binding IsChecked}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding FilterCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding FilterCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
除了
FilterCommand
之外,一切都运行良好.我的 C# 代码中有这个:public DelegateCommand<object> FilterCommand { get; set; }
...
FilterCommand = new DelegateCommand<object>(Filter);
Filter(object obj)
是一个函数,但是当我选中或取消选中我的任何复选框时,它不会被输入。任何帮助将不胜感激。
最佳答案
问题是,列表项的数据上下文是 FilterModel
类型。 ,但 FilterCommand
在父 View 模型中。
为命令尝试以下绑定(bind):
{Binding DataContext.FilterCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}
关于c# - WPF MVVM : EventTrigger is not working inside CheckBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45438801/