如何从DataGrid标题隐藏上下文菜单?

时间:2016-02-18 16:49:23

标签: wpf mvvm datagrid contextmenu

我在右键单击DataGrid时使用ContextMenu。我不想在DataGrid标题上显示ContextMenu。

<dgWPFCtrl:SelfBindingDataGrid Grid.Row="1" Margin="0,5,0,0" Name="_cycleList"
                           ItemsSource="{Binding Path=CurrentElementsList, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                           IsReadOnly="False"
                           SelectedItem="{Binding Path=CurrentItem}">
    <dgWPFCtrl:SelfBindingDataGrid.ContextMenu>
        <ContextMenu>
            <MenuItem Command="{Binding OpenCycleCommand}" Header="CYCLE" CommandParameter="CYCLE"/>
            <MenuItem Command="{Binding OpenCycleCommand}" Header="H1" CommandParameter="H1"/>
            <MenuItem Command="{Binding OpenCycleCommand}" Header="PDF" CommandParameter="PDF"/>
        </ContextMenu>
    </dgWPFCtrl:SelfBindingDataGrid.ContextMenu>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您可以仅使用ColumnHeaderStyle标题更改ContextMenu:

<dgWPFCtrl:SelfBindingDataGrid>
    <dgWPFCtrl:SelfBindingDataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu Visibility="Collapsed" />
                </Setter.Value>
            </Setter>
        </Style>
    </dgWPFCtrl:SelfBindingDataGrid.ColumnHeaderStyle>