Here is the sample code from my column chooser replacement (it was done before column chooser was available so there maybe a better way now). It should give you an idea of how to develop your own logic for the hide/unhide but feel free to ask if it is not clear.
In the XAML setup command bindings, the context menu and attach to the ColumnManagerCell
<!-- CommandBindings
for the Contextmenus in VPMPartList-->
<xcdg:DataGridControl.CommandBindings>
<CommandBinding Command=
"f:CLS_CustomCommands.HideGridColumn"
CanExecute=
"CommandHideGridColumn_CanExecute"
Executed=
"CommandHideGridColumn_Executed"/>
<CommandBinding Command=
"f:CLS_CustomCommands.UnHideSpecificGridColumn"
Executed=
"CommandUnHideSpecificGridColumn_Executed"/>
</xcdg:DataGridControl.CommandBindings>
<xcdg:DataGridControl.Resources>
<ResourceDictionary>
<ContextMenu x:Key=
"ColumnHeaderContextMenu" Opened="ColumnHeaderContextMenu_Opened">
<MenuItem Command=
"f:CLS_CustomCommands.HideGridColumn"
CommandTarget=
"{Binding RelativeSource={RelativeSource AncestorType={x:Type f:CLS_VPMPartListGrid}}}"
CommandParameter=
"{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget}"/>
<MenuItem Header=
"UnHide Specific Column" Tag="mnuHiddenColumnsList"/>
</ContextMenu>
<!-- ColumnManagerCell Style definition -->
<Style TargetType=
"{x:Type xcdg:ColumnManagerCell}">
<Setter Property=
"ContextMenu" Value="{StaticResource ColumnHeaderContextMenu}"/>
</Style>
</ResourceDictionary>
</xcdg:DataGridControl.Resources>
<xcdg:DataGridControl.View>
<xcdg:TableView ....
AllowColumnChooser=
"False">
</xcdg:TableView>
</xcdg:DataGridControl.View>
In the Code Behind Class...
virtual protected void ColumnHeaderContextMenu_Opened(object sender, RoutedEventArgs e)
{
try
{
ContextMenu ctlContextMenu = sender
as ContextMenu;
//See if it exists already
MenuItem mnuHiddenColumnsList =
null;
foreach (object contextMenuItem in ctlContextMenu.Items)
{
if (contextMenuItem is MenuItem)
{
MenuItem menuItem = contextMenuItem
as MenuItem;
if (menuItem.Tag != null && menuItem.Tag is string)
{
if ((string)menuItem.Tag == "mnuHiddenColumnsList")
{
mnuHiddenColumnsList = menuItem;
break;
}
}
}
}
//Now add the subMenu of the currently hidden columns
if (mnuHiddenColumnsList != null)
{
mnuHiddenColumnsList.Items.Clear();
CreateHiddenColumnMenuList(mnuHiddenColumnsList);
mnuHiddenColumnsList.IsEnabled = (mnuHiddenColumnsList.Items.Count > 0);
}
}
catch (Exception exception)
{
CLS_Global.GetGlobalSingleton().ExceptionHandler(exception);
}
}
virtual protected void CreateHiddenColumnMenuList(MenuItem mnuHiddenColumnsList)
{
//In here you could either list all the columns that are hidden, or just the group names of the columns
//that can be unhidden
if (mnuHiddenColumnsList != null)
{
//Now add the columns
List<ColumnBase> hiddenColumns =
new List<ColumnBase>();
foreach (ColumnBase thisColumn in Columns)
{
if (!thisColumn.Visible)
{
hiddenColumns.Add(thisColumn);
}
}
//Order the columns by the FieldName
List<ColumnBase> orderedHiddenColumns = hiddenColumns.OrderBy(c => c.Title).ToList();
foreach (ColumnBase hiddenColumn in orderedHiddenColumns)
{
MenuItem menuNewColumn =
new MenuItem();
string sColumnName = hiddenColumn.Title.ToString();
menuNewColumn.Header = sColumnName.Replace(
'\r', ' ');
menuNewColumn.Command = CLS_CustomCommands.UnHideSpecificGridColumn;
menuNewColumn.CommandParameter = hiddenColumn;
menuNewColumn.CommandTarget =
this;
mnuHiddenColumnsList.Items.Add(menuNewColumn);
}
}
}
//The Hide event handler. Could be adapted to hide mroe than one
protected void CommandHideGridColumn_Executed(object sender, ExecutedRoutedEventArgs e)
{
try
{
if (e.Parameter != null)
{
//Cannot hide the last column
if (e.Parameter is Cell && Columns.Count(c => c.Visible) > 1)
{
ColumnBase thisColumn = ((Cell)e.Parameter).ParentColumn;
thisColumn.Visible =
false;
}
}
}
catch (Exception exception)
{
CLS_Global.GetGlobalSingleton().ExceptionHandler(exception);
}
}
//The Unhide specific column - could be adapted to unhide more than one.
protected void CommandUnHideSpecificGridColumn_Executed(object sender, ExecutedRoutedEventArgs e)
{
try
{
if (e.Parameter != null)
{
if (e.Parameter is ColumnBase)
{
ColumnBase thisColumn = e.Parameter
as ColumnBase;
thisColumn.Visible =
true;
if (thisColumn.Width < 5)
{
thisColumn.Width = 50;
}
}
}
}
catch (Exception exception)
{
CLS_Global.GetGlobalSingleton().ExceptionHandler(exception);
}
}