0

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.

francosy
  • 173
  • 3
  • 12
  • Why not using the setter of `IsChecked` to call `Filter`? – JanDotNet Aug 01 '17 at 13:16
  • 2
    Look at the output window when debugging, do you have any binding errors? – ajg Aug 01 '17 at 13:17
  • Oh, yes, I have: System.Windows.Data Error: 40 : `BindingExpression path error: 'FilterCommand' property not found on 'object' ''FilterModel' (HashCode=57774494)'. BindingExpression:Path=FilterCommand; DataItem='FilterModel' (HashCode=57774494); target element is 'InvokeCommandAction' (HashCode=8505800); target property is 'Command' (type 'ICommand')` – francosy Aug 01 '17 at 13:22
  • @JanDotNet I have an object `FilterModel` in different file than `Filter` method. It cannot be static. Is there any other way to call it? – francosy Aug 01 '17 at 13:33
  • But the `FilterCommand` and the `IsChecked` properties are in the same class, isn't it`? – JanDotNet Aug 01 '17 at 13:35
  • @ajg Do you have any solution to that error? – francosy Aug 01 '17 at 13:38
  • What is DelegateCommand, are you using prism? – ajg Aug 01 '17 at 13:39
  • @JanDotNet No. I have class `FilterModel` with 2 attributes - `Name` and `IsChecked`. `FilterModel` is `DelegateCommand` in `MainViewModel` – francosy Aug 01 '17 at 13:40
  • @ajg Yes, I'm using prism. – francosy Aug 01 '17 at 13:40

2 Answers2

5

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}}}
JanDotNet
  • 3,746
  • 21
  • 30
1

this is work for me.

<DataGridTemplateColumn  Header="IsApved"  IsReadOnly="False" CanUserSort="False" Width="55">
                    <DataGridTemplateColumn.CellTemplate >
                        <DataTemplate>
                            <CheckBox 
                        Content="{Binding Name}"
                        IsChecked="{Binding IsSelect,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">

                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Unchecked">
                                        <i:InvokeCommandAction Command="{Binding DataContext.CheckCommand,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                    </i:EventTrigger>
                                    <i:EventTrigger EventName="Checked">
                                        <i:InvokeCommandAction Command="{Binding DataContext.CheckCommand,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '22 at 12:48