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

Context menu on header cell problem

1 Answer 271 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Boots
Top achievements
Rank 1
Boots asked on 08 Jan 2010, 07:35 PM
Hello,

I'm using the GridView/HeaderContextMenu example for version 2009.3.1208.35. When i have more columns than displayed on the screen it seems to mix up the contextmenus. Also this line of code IEnumerable<GridViewHeaderCell> cells = row.Cells.OfType<GridViewHeaderCell>(); in the InitializeMenus method doesn't return all of the GridViewHeaderCells.

made a quick example,
<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:telerk="http://schemas.telerik.com/2008/xaml/presentation" 
    xmlns:local="clr-namespace:WpfApplication1"
    <Grid> 
        <telerk:RadGridView x:Name="rgv" local:GridViewHeaderMenu.IsEnabled="True" /> 
    </Grid> 
</Window> 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Collections; 
 
namespace WpfApplication1 
    /// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
        public Window1() 
        { 
            InitializeComponent(); 
 
            ArrayList List = new ArrayList(); 
            for (int i = 0; i < 100; i++) 
            { 
                List.Add(new Testing() 
                { 
                    Test1 = i.ToString(), 
                    Test2 = i, 
                    Test3 = i, 
                    Test4 = i.ToString(), 
                    Test5 = i, 
                    Test6 = i, 
                    Test7 = i.ToString(), 
                    Test8 = i, 
                    Test9 = i, 
                    Test10 = i.ToString(), 
                    Test11 = i, 
                    Test12 = i, 
                    Test13 = i.ToString(), 
                    Test14 = i, 
                    Test15 = i, 
                    Test16 = i.ToString(), 
                    Test17 = i, 
                    Test18 = i 
                }); 
            } 
 
            rgv.ItemsSource = List; 
        } 
    } 
 
    public class Testing 
    { 
        public String Test1 { getset; } 
        public int Test2 { getset; } 
        public Double Test3 { getset; } 
        public String Test4 { getset; } 
        public int Test5 { getset; } 
        public Double Test6 { getset; } 
        public String Test7 { getset; } 
        public int Test8 { getset; } 
        public Double Test9 { getset; } 
        public String Test10 { getset; } 
        public int Test11 { getset; } 
        public Double Test12 { getset; } 
        public String Test13 { getset; } 
        public int Test14 { getset; } 
        public Double Test15 { getset; } 
        public String Test16 { getset; } 
        public int Test17 { getset; } 
        public Double Test18 { getset; } 
    } 
 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ComponentModel; 
using System.Text; 
using System.Windows; 
using System.Windows.Data; 
using Telerik.Windows; 
using Telerik.Windows.Controls; 
using Telerik.Windows.Controls.GridView; 
using Telerik.Windows.Data; 
 
namespace WpfApplication1 
    public class GridViewHeaderMenu 
    { 
        private 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
            { 
                this.grid.Loaded += new RoutedEventHandler(grid_Loaded); 
            } 
        } 
 
        void grid_Loaded(object sender, RoutedEventArgs e) 
        { 
            GridViewHeaderRow headerRow = grid.ChildrenOfType<GridViewHeaderRow>().FirstOrDefault(); 
            if (headerRow != null
            { 
                InitializeMenus(headerRow); 
            } 
        } 
 
        private void InitializeMenus(GridViewHeaderRow row) 
        { 
            IEnumerable<GridViewHeaderCell> cells = row.Cells.OfType<GridViewHeaderCell>(); 
 
            foreach (GridViewHeaderCell cell in cells) 
            { 
                // create menu 
                RadContextMenu contextMenu = new RadContextMenu(); 
                // set menu Theme 
                StyleManager.SetTheme(contextMenu, StyleManager.GetTheme(grid)); 
 
                RadMenuItem item = new RadMenuItem() { Header = String.Format(@"Sort Ascending by ""{0}""", cell.Column.Header) }; 
                contextMenu.Items.Add(item); 
 
                item = new RadMenuItem() { Header = String.Format(@"Sort Descending by ""{0}""", cell.Column.Header) }; 
                contextMenu.Items.Add(item); 
 
                item = new RadMenuItem() { Header = String.Format(@"Clear Sorting by ""{0}""", cell.Column.Header) }; 
                contextMenu.Items.Add(item); 
 
                item = new RadMenuItem() { Header = String.Format(@"Group by ""{0}""", cell.Column.Header) }; 
                contextMenu.Items.Add(item); 
 
                item = new RadMenuItem() { Header = String.Format(@"Ungroup ""{0}""", cell.Column.Header) }; 
                contextMenu.Items.Add(item); 
 
                item = new RadMenuItem() { Header = "Choose Columns:" }; 
                contextMenu.Items.Add(item); 
 
                // create menu items 
                foreach (GridViewColumn column in grid.Columns) 
                { 
                    RadMenuItem subMenu = new RadMenuItem() 
                    { 
                        Header = column.Header, 
                        IsCheckable = true
                        IsChecked = true 
                    }; 
 
                    // bind IsChecked menu item property to IsVisible column property 
                    subMenu.SetBinding(RadMenuItem.IsCheckedProperty, 
                        new Binding("IsVisible") { Mode = BindingMode.TwoWay, Source = column }); 
 
                    item.Items.Add(subMenu); 
                } 
 
                contextMenu.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick)); 
 
                // attach menu 
                RadContextMenu.SetContextMenu(cell, contextMenu); 
            } 
        } 
 
        void OnMenuItemClick(object sender, RoutedEventArgs e) 
        { 
            RadContextMenu menu = (RadContextMenu)sender; 
            RadMenuItem clickedItem = ((RadRoutedEventArgs)e).OriginalSource as RadMenuItem; 
            GridViewColumn column = ((GridViewHeaderCell)menu.UIElement).Column; 
 
            if (clickedItem.Parent is RadMenuItem) 
                return
 
            string header = Convert.ToString(clickedItem.Header); 
 
            using (grid.DeferRefresh()) 
            { 
                Telerik.Windows.Data.SortDescriptor sd = (from d in grid.SortDescriptors 
                                                          where d.Member == column.UniqueName 
                                                          select d).FirstOrDefault(); 
 
                if (header.Contains("Sort Ascending")) 
                { 
                    if (sd != null
                    { 
                        grid.SortDescriptors.Remove(sd); 
                    } 
 
                    grid.SortDescriptors.Add(new Telerik.Windows.Data.SortDescriptor() 
                    { 
                        Member = column.UniqueName, 
                        SortDirection = ListSortDirection.Ascending 
                    }); 
                } 
                else if (header.Contains("Sort Descending")) 
                { 
                    if (sd != null
                    { 
                        grid.SortDescriptors.Remove(sd); 
                    } 
 
                    grid.SortDescriptors.Add(new Telerik.Windows.Data.SortDescriptor() 
                    { 
                        Member = column.UniqueName, 
                        SortDirection = ListSortDirection.Descending 
                    }); 
                } 
                else if (header.Contains("Clear Sorting")) 
                { 
                    if (sd != null
                    { 
                        grid.SortDescriptors.Remove(sd); 
                    } 
                } 
                else if (header.Contains("Group by")) 
                { 
                    Telerik.Windows.Data.GroupDescriptor gd = (from d in grid.GroupDescriptors 
                                                               where d.Member == column.UniqueName 
                                                               select d).FirstOrDefault(); 
 
                    if (gd == null
                    { 
                        grid.GroupDescriptors.Add(new Telerik.Windows.Data.GroupDescriptor() { Member = column.UniqueName, SortDirection = ListSortDirection.Ascending }); 
                    } 
                } 
                else if (header.Contains("Ungroup")) 
                { 
                    Telerik.Windows.Data.GroupDescriptor gd = (from d in grid.GroupDescriptors 
                                                               where d.Member == column.UniqueName 
                                                               select d).FirstOrDefault(); 
                    if (gd != null
                    { 
                        grid.GroupDescriptors.Remove(gd); 
                    } 
                } 
            } 
        } 
    } 
 

on my machine if you scroll to the right an open the contextmenu for column Test17 it opens up the contextmenu for Test3.

Any help would be great.
Thanks Again,
~Boots



1 Answer, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 14 Jan 2010, 07:28 AM
Hi Boots,

The problem you have described is due to our horizontal virtualization and container recycling we have implemented in 2009 Q3 version. One way to solve it is to switch off the just the column virtualization by setting EnableColumnVirtualization property to false on RadGridView. The other way is to implement the header context menu directly on your columns using the Header property of the column.

Hope this helps,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Boots
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
Share this question
or