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.

9
  • Why not using the setter of IsChecked to call Filter? Commented Aug 1, 2017 at 13:16
  • 2
    Look at the output window when debugging, do you have any binding errors? Commented Aug 1, 2017 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') Commented Aug 1, 2017 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? Commented Aug 1, 2017 at 13:33
  • But the FilterCommand and the IsChecked properties are in the same class, isn't it`? Commented Aug 1, 2017 at 13:35

2 Answers 2

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}}}
Sign up to request clarification or add additional context in comments.

Comments

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>

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.