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.