如何在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>
如何为每列创建ContextMenu?
例如,当上下文菜单单击第一列(单元格属于第一列)时:它显示“Column1菜单”项和 当它点击第二列(在单元格属于第二列)时显示“Column2菜单”项目
答案 0 :(得分: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(...)
{}
}
}