In my WPF application I have a DataGrid and I started adding a "filter" button that opens a small popup TextBlock just under it. The desired outcome is that the user can click on the filter button, see the menu, click on any of the selections and the datagrid should update accordingly.
So far I got the filter button working and when I click on it it does bring up the little pop up menu. But the problem is that I am unable to get the SelectionChanged event to fire up.
Here is how I have my filter button set up in Xaml:
<DataGrid>
<DataGrid.Resources>
<DataTemplate x:Key="FooHeaderTemplate">
<Grid Margin="0, 0, -5, 0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ContentControl Content="{Binding}" VerticalAlignment="Center"/>
<ToggleButton Name="FooFilterButton"
Grid.Column="1"
Width="20"
Height="20"
Padding="1, 0" BorderBrush="{x:Null}" Foreground="{x:Null}" Background="{x:Null}">
<ToggleButton.Content>
<Image Stretch="Fill">
<Image.Style>
<Style TargetType="Image">
<Style.Triggers>
<DataTrigger
Binding="{Binding DataContext.IsFooFilterApplied, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="True">
<Setter Property="Source" Value="/Foo.Bar.MyTestProject;component/Resources/FilterFull_64x64.png"/>
</DataTrigger>
<DataTrigger
Binding="{Binding DataContext.IsFooFilterApplied, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="False">
<Setter Property="Source" Value="/Foo.Bar.MyTestProject;component/Resources/FilterEmpty_64x64.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ToggleButton.Content>
</ToggleButton>
<Popup IsOpen="{Binding ElementName=FooFilterButton, Path=IsChecked}" PlacementTarget="{Binding ElementName=FooFilterButton}" Placement="Bottom" StaysOpen="False" HorizontalAlignment="Right">
<Border Background="White" Padding="3">
<Grid>
<ListBox x:Name="MyListBox"
SelectionMode="Multiple"
SelectionChanged="MyListBox_SelectionChanged"
ItemsSource="{Binding DataContext.MyFilterList,
Mode=OneWay,
UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Border>
</Popup>
</Grid>
</DataTemplate>
</DataGrid.Resources>
</DataGrid>
In my code behind I have my SelectionChanged event set up as:
private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
if (listBox == null) return;
_viewModel.SelectedFilters.Clear();
foreach (string item in listBox.SelectedItems)
{
_viewModel.SelectedFilters.Add(item);
}
_viewModel.FilterData();
}
The problem is that this event is not being hit when I click on any of the checkboxes. Can anyone help me understand what I am doing wrong? Many thanks in advance.