I have a ContextMenu inside DataGridTemplateColumn, the menuitems are all the production countries for the view. I am using this to filter away countries.
Here is code:
<DataGridTemplateColumn SortMemberPath="ProductionCountry" x:Name="prodCountryColumn" Width="Auto" CanUserSort="True">
<DataGridTemplateColumn.Header>
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock.ContextMenu x:Name="cmProdCountry" >
<ContextMenu Loaded="ContextMenu_Loaded" ItemsSource="{Binding Path=FilterProdCountry}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Name="prodCountryFilter" IsCheckable="True" Checked="toggleFilterOn" Unchecked="toggleFilterOff" Header="{Binding}" ItemsSource="{Binding}">
</MenuItem>
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</TextBlock.ContextMenu>
Produksjonsland
</TextBlock>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Padding="5,1,5,1" Text="{Binding Path=ProductionCountry}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
To remove all filtering I have a listing that sais "Show all".
The menuitems are checkable and here is my question: How can I find and uncheck all menuitems when the menuitem "Show all" is checked.
In code behind I use an getancestor function that gets me to the ContextMenu but all the items are only listed as strings so I can't set the MenuItem.IsChecked = false;
So when I try to find all menuitems for unchecking in codebehind i get exception.
Here is code:
var filterItem = (sender as MenuItem);
var parent = filterItem.FindAncestorTest<TextBlock>();
foreach (var menuitem in parent.Items)
{
(mi as MenuItem).IsChecked = false;
}