This question is locked. New answers and comments are not allowed.
Hello,
I am looking for some help with an issue concerning columns that are created with the IsVisible property set to false, then turned having their IsVisible properties set to true via a context menu. The initial code was created using code from the Telerik demo "Gridview/HeaderContextMenu" as a basis. The aim is to have columns that can be turned on or off with a menu, with some defaulted to on and some off. The ones defaulted to on work with no problem, the problem, however, is the ones defaulted to off do not appear when their IsVisible properties are set to true. However, they will finally appear if I switch tabs but even then the colum headers are missing.
I would appreciate any help I may recieve in understanding this problem and finding a solution. Below is the code for my context menu, and I can provide more if needed. To me it seems the overall way in which the gridview was implemented was very similiar to how the Telerik demo was, with the exception that the code begins with columns being set to hidden. That said, perhaps I am missing some important subtleties in what needs to be done to make this work correctly.
Thank everyone for their time,
Philip Coffey
public class GridViewContextMenu
{
public GridViewContextMenu(GridView gridView)
{
this.GridView = gridView;
}
private GridView GridView { get; set; }
public static readonly DependencyProperty IsEnabledProperty
= DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(GridViewContextMenu),
new Telerik.Windows.PropertyMetadata(new PropertyChangedCallback(OnIsEnabledPropertyChanged)));
public static void SetIsEnabled(DependencyObject dependencyObject, bool enabled)
{
dependencyObject.SetValue(IsEnabledProperty, enabled);
}
private static void OnIsEnabledPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
GridView gridView = dependencyObject as GridView;
if (gridView != null)
{
if ((bool)e.NewValue)
{
if (gridView.IsFilteringAllowed)
{
GridViewContextMenu menu = new GridViewContextMenu(gridView);
menu.Initialize();
}
}
}
}
private void Initialize()
{
RadContextMenu contextMenu = new RadContextMenu();
StyleManager.SetTheme(contextMenu, StyleManager.GetTheme(this.GridView));
RadMenuItem item = new RadMenuItem() { Header = "Add/Remove Columns" };
contextMenu.Items.Add(item);
foreach (GridViewColumn column in this.GridView.Columns)
{
RadMenuItem subMenu = new RadMenuItem()
{
Header = column.HeaderText,
IsCheckable = true,
IsChecked = true,
Tag = column
};
subMenu.SetBinding(RadMenuItem.IsCheckedProperty, new Binding("IsVisible") { Mode = BindingMode.TwoWay, Source = column });
subMenu.Click += new RoutedEventHandler(subMenu_Click);
item.Items.Add(subMenu);
}
RadContextMenu.SetContextMenu(this.GridView, contextMenu);
}
}
I am looking for some help with an issue concerning columns that are created with the IsVisible property set to false, then turned having their IsVisible properties set to true via a context menu. The initial code was created using code from the Telerik demo "Gridview/HeaderContextMenu" as a basis. The aim is to have columns that can be turned on or off with a menu, with some defaulted to on and some off. The ones defaulted to on work with no problem, the problem, however, is the ones defaulted to off do not appear when their IsVisible properties are set to true. However, they will finally appear if I switch tabs but even then the colum headers are missing.
I would appreciate any help I may recieve in understanding this problem and finding a solution. Below is the code for my context menu, and I can provide more if needed. To me it seems the overall way in which the gridview was implemented was very similiar to how the Telerik demo was, with the exception that the code begins with columns being set to hidden. That said, perhaps I am missing some important subtleties in what needs to be done to make this work correctly.
Thank everyone for their time,
Philip Coffey
public class GridViewContextMenu
{
public GridViewContextMenu(GridView gridView)
{
this.GridView = gridView;
}
private GridView GridView { get; set; }
public static readonly DependencyProperty IsEnabledProperty
= DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(GridViewContextMenu),
new Telerik.Windows.PropertyMetadata(new PropertyChangedCallback(OnIsEnabledPropertyChanged)));
public static void SetIsEnabled(DependencyObject dependencyObject, bool enabled)
{
dependencyObject.SetValue(IsEnabledProperty, enabled);
}
private static void OnIsEnabledPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
GridView gridView = dependencyObject as GridView;
if (gridView != null)
{
if ((bool)e.NewValue)
{
if (gridView.IsFilteringAllowed)
{
GridViewContextMenu menu = new GridViewContextMenu(gridView);
menu.Initialize();
}
}
}
}
private void Initialize()
{
RadContextMenu contextMenu = new RadContextMenu();
StyleManager.SetTheme(contextMenu, StyleManager.GetTheme(this.GridView));
RadMenuItem item = new RadMenuItem() { Header = "Add/Remove Columns" };
contextMenu.Items.Add(item);
foreach (GridViewColumn column in this.GridView.Columns)
{
RadMenuItem subMenu = new RadMenuItem()
{
Header = column.HeaderText,
IsCheckable = true,
IsChecked = true,
Tag = column
};
subMenu.SetBinding(RadMenuItem.IsCheckedProperty, new Binding("IsVisible") { Mode = BindingMode.TwoWay, Source = column });
subMenu.Click += new RoutedEventHandler(subMenu_Click);
item.Items.Add(subMenu);
}
RadContextMenu.SetContextMenu(this.GridView, contextMenu);
}
}