c# - WPF MVVM : EventTrigger is not working inside CheckBox

标签 c# wpf checkbox mvvm eventtrigger

我正在用 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/

相关文章:

c# - 小计关系数据表

c# - WPF中BackgroundWorker线程如何直接访问UI线程?

android - 在 PreferenceActivity 中膨胀复选框时出错

javascript - jQuery 不适用于只选择一个复选框

c# - 如何在c#中使用循环调用不同名称的数组

c# - 如何检测当前按下的键?

c# - MVC5 项目中 exe 文件的用途是什么?

wpf - Fluent(Codeplex) Ribbon 中关闭、最大化、最小化按钮的自动化 - WPF 应用程序

wpf - Generic.xaml 有什么特别之处?

jquery - 如何使用 jQuery 从表行中的特定单元格获取复选框值?