RowDefinition的WPF ContextMenu未显示

时间:2017-02-03 09:22:39

标签: c# wpf grid

var gridRow = new RowDefinition();
gridRow.Height = GridLength.Auto; 

var context = new ContextMenu();
context.Items.Add("Remove");  

gridRow.ContextMenu = context;
mainGrid.RowDefinitions.Add(gridRow);

当我右键单击行时,这不会打开ContextMenu,但这可以正常工作

this.mainGrid.ContextMenu = context;

那么如何让它在行上工作?

谢谢

1 个答案:

答案 0 :(得分:0)

您找不到基于行的上下文菜单。最好提供基于网格的上下文菜单,使用命令,如

<DataGrid AutoGenerateColumns="True"
        Name="myGrid"
        ItemsSource="{Binding Orders}">
    <DataGrid.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Copy" Command="{Binding CopyItem}" />
            <MenuItem Header="Delete" Command="{Binding DeleteItem}" />
        </ContextMenu>
    </DataGrid.ContextMenu>
</DataGrid>

命令设置如下:

VM.DeleteItem 
     = new OperationCommand((o) => MessageBox.Show("Delete Me"),
                            (o) => (myGrid.SelectedItem as Order)?.InProgress == false );

enter image description here