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.
But how do i save the condition, which column is visible and which one not?
Thanks
Best Regards
Rene
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