Search code examples
c#wpfdatagridcontextmenudevexpress-wpf

How to make context menu to each column cell at devexpress datagrid?


How to make context menu to each column cell at devexpress datagrid?

   xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
  <dxg:GridControl  Grid.Row="0"  
                                  Height="150"                                      
                                  ItemsSource="{Binding ObjectViewModel.Collection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                  SelectedItem="{Binding CurrentRow,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,TargetNullValue=null}"
                                  >
                    <dxg:GridControl.Columns>

                        <dxg:GridColumn Header="Column1"}"                                                
                                            Width="*"                                            
                                            Binding="{Binding col1,Mode=OneWay}">
                        </dxg:GridColumn>
                        <dxg:GridColumn   Header="Column2"}
                                            AllowEditing="False"                                          
                                            Width="*"
                                            Binding="{Binding col2,Mode=OneWay}">
                            <!--<dxg:GridColumn.CellStyle>
                                <Style TargetType="DataGridCell">
                                    <Setter Property="ContextMenu">
                                        <Setter.Value>
                                            <ContextMenu>
                                                <MenuItem Header="Col2!"></MenuItem>
                                            </ContextMenu>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </dxg:GridColumn.CellStyle>-->
                        </dxg:GridColumn>                    
                </dxg:GridControl>

How to make ContextMenu for each column?

For example, when context menu click on first column (on cell belongs to first column): it show "Column1 menu" item and when it click on second column (on cell belongs to second column) is show "Column2 menu" item


Solution

  • I found answer:

     <dxg:GridControl.View>
                            <!--region #RowCellMenuCustomization-->
                            <dxg:TableView x:Name="view" AutoWidth="True">                             
                                <dxg:TableView.RowCellMenuCustomizations>
    
                                    <dxb:BarButtonItem  Content="Menu1"                                           
                                           ItemClick="CellDataItem_Menu1_ItemClick"
                                                      Glyph="/MyCompany.UI.Resources;component/PNGImages/Menu1.png"
                                                       >
                                   </dxb:BarButtonItem>                                                                      
                                </dxg:TableView.RowCellMenuCustomizations>
                            </dxg:TableView>
    

    And handler:

    private void CellDataItem_ByColumn_ItemClick(object sender, ItemClickEventArgs e)
        {
            var menuInfo = view.GridMenu.MenuInfo as GridCellMenuInfo;
            if (menuInfo != null && menuInfo.Row != null)
            {
                var column = menuInfo.Column as GridColumn;               
                if (column == null) return;
    
                if ((String)column.ActualColumnChooserHeaderCaption == _nameColumn)//context menu under column "Name"
                {
                    //code here
                }
                else if(...)
                {}
    
            }
        }