I am using Infragistics default style for XamContextMenu, after setting the context menu as follows,
<ig:XamContextMenu Name="_navigatorContextMenu" Style="{StaticResource DarkContextMenuStyle}" ItemContainerStyle="{StaticResource PurpleMenuItemStyle}" Opening="XamContextMenu_Opening">
menu item checkboxes no longer checkable. If I remove the ItemContainerStyle="{StaticResource PurpleMenuItemStyle}", they are again checkable.
I have attached sample showing this issue. Run the sample and right-click the menu to see the context menu. All the items should show initially checked but none of them are and user cannot click to change the state of the checkbox. If you remove the ItemContainerStyle="{StaticResource PurpleMenuItemStyle}" from the XamContextMenu XAML you will see the checkmarks.
I tried playing with the section (below) in default style supplied by Infragistics but can't get the behavior to function correctly.
<ContentPresenter x:Name="HeaderCheckboxPresenter" Margin="4,3,4,3" HorizontalAlignment="Right" VerticalAlignment="Center" Visibility="{TemplateBinding CheckBoxVisibilityResolved}"> <ContentPresenter.ContentTemplate> <DataTemplate> <CheckBox IsHitTestVisible="False" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked, Mode=TwoWay}"/> </DataTemplate> </ContentPresenter.ContentTemplate> </ContentPresenter>
Please see attached file XamMenuFun.zip for the sample demonstrating the issue.
Thank you for any help.
--Pat
Hello Pat,
Yes, you can change the XAML and the exception will no longer be thrown. The reason to throw the exception is that setting ItemContainerStyle will force the application to apply the style to each and every item that is in the Items property of the context menu. Since you add to that collection a separator, the style is set to the separator and then the exception comes. The style targets XamMenuItem, not Separator.
To solve the issue, do the following:
1) Remove the ItemContainerStyle setter in the context menu in XAML.
2) In XamContextMenu_Opening before creating the menu items for the context menu, add the following line:
Style menuItemStyle = this.Resources["PurpleMenuItemStyle"] as Style;
3) Set the Style property of the menu items only (and not the separator):
contextMenuItem.Style = menuItemStyle;
This should solve the issue.
Let me know if you need any other assistance.
Sincerely,
Lazar Nikolov
Infragistics
www.infragistics.com/support
Thank you Lazar. That works correctly. I have one more question that I can't figure out. If you look at the code behind (MainWindow.xaml.cs) in the XamContextMenu_Opening event handler, in the section where I am adding a separator, I have,
// Add a seperator XamMenuSeparator separator = new XamMenuSeparator(); separator.Style = null; // Need this or else exceptions contextMenu.Items.Add(separator);
I had to add the separator.Style = null or else I get exceptions due to the use of the default styling (if you comment out this line you can see the exception). Is there something that can be done in the xaml to avoid the framework trying to apply the style to the XamMenuSepartor?
Thank you for any assistance.
Hi Pat,
To fix the issue you need to make two changes to your code:
1) You've set IsHitTestVisible to False in the check box inside PurpleMenuItemStyle (line 283). You can remove this property setter or change it to True.
2) There is an issue with the binding that you are using for the Checkbox element. You need to use Ancestor binding because the parent element is ContentPresenter.
After making those two changes, the check box should look like this:
<CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ig:XamMenuItem}}, Path=IsChecked, Mode=TwoWay}"/>
Let me know if you have any other questions.