Hi,
I'm using the following approach for adding field chooser context menus for column headers:
public class ExtendedGrid : XamDataGrid, IColumnLayout{ protected override void OnFieldLayoutInitialized(FieldLayoutInitializedEventArgs args) { base.OnFieldLayoutInitialized(args); this.SetupColumnMenus(); foreach (FieldLayout colLayout in this.FieldLayouts) { colLayout.Fields.CollectionChanged += this.OnFieldsChanged; } }
private void OnFieldsChanged(object sender, NotifyCollectionChangedEventArgs e) { this.SetupColumnMenus(); }
private void SetupColumnMenus() { Style originalStyle = this.FindResource(typeof(LabelPresenter)) as Style; foreach (FieldLayout colLayout in this.FieldLayouts) { ContextMenu columnSubMenu = new ContextMenu(); Style headerPresenterStyle = new Style(typeof(DataRecordPresenter)); Trigger isHeaderRecordTrigger = new Trigger { Property = DataRecordPresenter.IsHeaderRecordProperty, Value = true }; headerPresenterStyle.Triggers.Add(isHeaderRecordTrigger); isHeaderRecordTrigger.Setters.Add(new Setter(ContextMenuProperty, columnSubMenu)); colLayout.Settings.DataRecordPresenterStyle = headerPresenterStyle; Style newSubStyle = new Style(typeof(LabelPresenter)); newSubStyle.BasedOn = originalStyle; newSubStyle.Setters.Add(new Setter(ContextMenuProperty, columnSubMenu)); foreach (Field col in colLayout.Fields.OrderBy(x => x.Label)) { SetupColumnContextMenu(newSubStyle, columnSubMenu, col); } } }
private static void SetupColumnContextMenu(Style styleWithContextMenu, ItemsControl columnMenu, Field col) { if (col == null || col.Settings == null) { return; } if (!col.HasLabel) { return; } col.Settings.LabelPresenterStyle = styleWithContextMenu; MenuItem item = new MenuItem() { Header = col.Label }; item.IsChecked = col.Visibility == Visibility.Visible; BindingOperations.SetBinding(col, Field.VisibilityProperty, new Binding() { Source = item, Path = new PropertyPath(MenuItem.IsCheckedProperty), Converter = new BooleanToVisibilityConverter(), Mode = BindingMode.OneWay }); item.IsCheckable = true; columnMenu.Items.Add(item); }}
This approch works nicely, however, one issue surfaced and I don't know why. Sometimes, the context menu just stops working. When you right click a column header, nothing happens. It starts working again if you expand a collapsed record and make OnFieldLayoutInitialized() fire again when a child field layout is loaded. So it seems to me that the grid loses track of the DataRecordPresenterStyle and LabelPresenterStyle. But for what reason? Is there some kind "recycling" / container generation of header controls taking place that causes such issues? Or anything else I am missing?
Regards,
Florian
Hello Florian,
Thank you for your post.
I have been looking into your issue. I have been trying to create a sample application from the code snippet you have provided. It seems there are some custom code and I am not able to create a sample.
Would you please provide me with a small isolated sample application with the functionality you are using, in order for me to be able to further investigate this issue for you?
Thank you in advance for the cooperation. Looking forward to hearing from you.
Actually, you can just copy and paste the code. I made a test project anyway.
The problem is that the issue is not easily reproducible so I need to tackle it from a theoretical point of view at the moment. Not sure if a sample project is really going to help you with the analysis.
I am just checking your progress on this issue. Please do not hesitate to let me know if you have any further questions on this matter.
Thank you for the feedback. I am looking forward to hearing from you.
Thanks, but in this case I need to leave it at that. I will post when I find out more info about the cause of the issue.
I have been testing the sample application that you have attached and I was not able to reproduce the issue you have mentioned.
We haven’t experienced any similar issue before. In order to assist you further I would need more details, like when and how this happens. Any steps to reproduce it and settings, specific styles, triggers or wherever you have set in your grid. Please note that having more information will help us to speed up the process and providing you more accurate help.
Looking forward to hearing from you.
We're on Windows 7 SP1, using 12.1.20121.2169 of Infragistics. However, as I said, I can't reproduce the issue either, it just happens occasionally for no apparent reason, both on developer and customer systems.