1

如何在 devexpress 数据网格中为每个列单元格制作上下文菜单?

   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>

如何为每一列制作 ContextMenu?

例如,当上下文菜单单击第一列(单元格上属于第一列)时:它显示“Column1 菜单”项,当它单击第二列时(单元格上属于第二列)显示“Column2 菜单”项

4

1 回答 1

1

我找到了答案:

 <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>

和处理程序:

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(...)
            {}

        }
    }
于 2017-03-20T07:40:32.577 回答