This is a migrated thread and some comments may be shown as answers.

Context Header Menu Example - Show Columns

1 Answer 273 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ITA
Top achievements
Rank 1
ITA asked on 22 Dec 2014, 02:54 PM
Hi,

i use a RadGridView and your "Header Context Menu" Example, where you can show or hide Columns. I have 10 Columns, 5 of them are
"IsVisible = False". On right click of GridViewHeader i can choose the other 5 columns. This works fine.

public class GridViewHeaderMenu
    {
        private readonly RadGridView grid = null;
 
        public GridViewHeaderMenu(RadGridView grid)
        {
            this.grid = grid;
        }
 
        public static readonly DependencyProperty IsEnabledProperty
           = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(GridViewHeaderMenu),
               new PropertyMetadata(new PropertyChangedCallback(OnIsEnabledPropertyChanged)));
 
        public static void SetIsEnabled(DependencyObject dependencyObject, bool enabled)
        {
            dependencyObject.SetValue(IsEnabledProperty, enabled);
        }
 
        public static bool GetIsEnabled(DependencyObject dependencyObject)
        {
            return (bool)dependencyObject.GetValue(IsEnabledProperty);
        }
 
        private static void OnIsEnabledPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            RadGridView grid = dependencyObject as RadGridView;
            if (grid != null)
            {
                if ((bool)e.NewValue)
                {
                    // Create new GridViewHeaderMenu and attach RowLoaded event.
                    GridViewHeaderMenu menu = new GridViewHeaderMenu(grid);
                    menu.Attach();
                }
            }
        }
 
        private void Attach()
        {
            if (grid != null)
            {
                // create menu
                RadContextMenu contextMenu = new RadContextMenu();
                // set menu Theme
                StyleManager.SetTheme(contextMenu, StyleManager.GetTheme(grid));
 
                contextMenu.Opened += OnMenuOpened;
                contextMenu.ItemClick += OnMenuItemClick;
 
                RadContextMenu.SetContextMenu(grid, contextMenu);
            }
        }
 
        void OnMenuOpened(object sender, RoutedEventArgs e)
        {
            RadContextMenu menu = (RadContextMenu)sender;
            GridViewHeaderCell cell = menu.GetClickedElement<GridViewHeaderCell>();
 
            if (cell != null)
            {
                menu.Items.Clear();
 
                RadMenuItem item = new RadMenuItem();
                item.Header = String.Format(@Klassen.Language.GetTextbyCode("SortAufsteigend") + cell.Column.Header);
                menu.Items.Add(item);
 
                item = new RadMenuItem();
                item.Header = String.Format(@Klassen.Language.GetTextbyCode("SortAbsteigend") + cell.Column.Header);
                menu.Items.Add(item);
 
                item = new RadMenuItem();
                item.Header = String.Format(@Klassen.Language.GetTextbyCode("SortClear") + cell.Column.Header);
                menu.Items.Add(item);
                 
                item = new RadMenuItem();
                item.Header = Klassen.Language.GetTextbyCode("ChooseColumns");
                menu.Items.Add(item);
 
                // create menu items
                foreach (GridViewColumn column in grid.Columns)
                {
                    RadMenuItem subMenu = new RadMenuItem();
                    subMenu.Header = column.Header;
                    subMenu.IsCheckable = true;
                    subMenu.IsChecked = true;
 
                    Binding isCheckedBinding = new Binding("IsVisible");
                    isCheckedBinding.Mode = BindingMode.TwoWay;
                    isCheckedBinding.Source = column;
 
                    // bind IsChecked menu item property to IsVisible column property
                    subMenu.SetBinding(RadMenuItem.IsCheckedProperty, isCheckedBinding);
 
                    item.Items.Add(subMenu);
                }
            }
            else
            {
                menu.IsOpen = false;
            }
        }
 
        void OnMenuItemClick(object sender, RoutedEventArgs e)
        {
            RadContextMenu menu = (RadContextMenu)sender;
 
            GridViewHeaderCell cell = menu.GetClickedElement<GridViewHeaderCell>();
            RadMenuItem clickedItem = ((RadRoutedEventArgs)e).OriginalSource as RadMenuItem;
            GridViewColumn column = cell.Column;
             
            if (clickedItem.Parent is RadMenuItem)
                return;
 
            string header = Convert.ToString(clickedItem.Header);
 
            using (grid.DeferRefresh())
            {
                ColumnSortDescriptor sd = (from d in grid.SortDescriptors.OfType<ColumnSortDescriptor>()
                                           where object.Equals(d.Column, column)
                                           select d).FirstOrDefault();
 
                if (header.Contains("Sort Ascending"))
                {
                    if (sd != null)
                    {
                        grid.SortDescriptors.Remove(sd);
                    }
 
                    ColumnSortDescriptor newDescriptor = new ColumnSortDescriptor();
                    newDescriptor.Column = column;
                    newDescriptor.SortDirection = ListSortDirection.Ascending;
 
                    grid.SortDescriptors.Add(newDescriptor);
                }
                else if (header.Contains("Sort Descending"))
                {
                    if (sd != null)
                    {
                        grid.SortDescriptors.Remove(sd);
                    }
 
                    ColumnSortDescriptor newDescriptor = new ColumnSortDescriptor();
                    newDescriptor.Column = column;
                    newDescriptor.SortDirection = ListSortDirection.Descending;
 
                    grid.SortDescriptors.Add(newDescriptor);
                }
                else if (header.Contains("Clear Sorting"))
                {
                    if (sd != null)
                    {
                        grid.SortDescriptors.Remove(sd);
                    }
                }
            }
        }
    }

But how do i save the condition, which column is visible and which one not?

Thanks
Best Regards
Rene

1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 23 Dec 2014, 10:46 AM
Hi Rene,

You can check our PersistenceFramework control on how to save the properties of your UI elements.

As to saving RadGridView's settings, you may check the "GridView Serialization" WPF Demo. You can also find the "GridView serialization" github example on how to persist control's settings.
Although GitHub is a very well-known platform we saw a better and easier approach for reviewing our examples developing our brand new SDK Samples Browser. You can install it and then use it to review the examples.

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
ITA
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or