WPF MVVM:EventTrigger在CheckBox中不起作用 [英] WPF MVVM: EventTrigger is not working inside CheckBox
问题描述
我正在使用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)
是一项功能,但是当我选中或取消选中任何CheckBox时都不会输入.
任何帮助将不胜感激.
问题是,列表项的数据上下文是FilterModel
类型,但是FilterCommand
在父视图模型中. >
尝试以下命令绑定:
{Binding DataContext.FilterCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}
I'm building an application with WPF in MVVM style. I'm trying to make filter on my DataGrid
when I check or uncheck several CheckBoxes for filtering.
I've found solution with Interaction.Triggers
, but it's not working for me in this case.
Here is my code:
<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>
Everything is working great except FilterCommand
. I have this in my C# code:
public DelegateCommand<object> FilterCommand { get; set; }
...
FilterCommand = new DelegateCommand<object>(Filter);
Filter(object obj)
is a function, but it is not entered when I check or uncheck any of my CheckBoxes.
Any help would be very appreciated.
The problem is, that the data context of the list item is of type FilterModel
, but the FilterCommand
is in the parent view model.
Try the following binding for the command:
{Binding DataContext.FilterCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}
这篇关于WPF MVVM:EventTrigger在CheckBox中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!