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,
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
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 { get; set; } |
public int Test2 { get; set; } |
public Double Test3 { get; set; } |
public String Test4 { get; set; } |
public int Test5 { get; set; } |
public Double Test6 { get; set; } |
public String Test7 { get; set; } |
public int Test8 { get; set; } |
public Double Test9 { get; set; } |
public String Test10 { get; set; } |
public int Test11 { get; set; } |
public Double Test12 { get; set; } |
public String Test13 { get; set; } |
public int Test14 { get; set; } |
public Double Test15 { get; set; } |
public String Test16 { get; set; } |
public int Test17 { get; set; } |
public Double Test18 { get; set; } |
} |
} |
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