如何在Listview标题上禁用上下文菜单

时间:2014-05-01 13:24:07

标签: c# wpf mvvm contextmenu

我有一个上下文菜单,当我右键单击列表视图的标题和空白区域时会显示该菜单。如何在列表视图的空白区域显示上下文菜单,但不在标题上显示。

        <ListView.View>
            <GridView AllowsColumnReorder="True" 
                <GridViewColumn Width="100" Header="Company" DisplayMemberBinding="{Binding Company}"  />
                <GridViewColumn Width="100" Header="Depatment"  DisplayMemberBinding="{Binding Department}" />
                <GridViewColumn Width="100" Header="Office" DisplayMemberBinding="{Binding Office}" />
            </GridView>
        </ListView.View>
        <ListView.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Add" Command="{Binding Add}"></MenuItem>
            </ContextMenu>
        </ListView.ContextMenu>

1 个答案:

答案 0 :(得分:1)

如果将columnheader上下文菜单的值设置为null,即。 ColumnHeaderContextMenu="{x:Null}"它不会工作...... 但你可以像这样破解它

<GridView.ColumnHeaderContextMenu>
                    <ContextMenu>
                        <ContextMenu.Template>
                            <ControlTemplate>
                                <Border BorderBrush="Transparent"
                                        Background="Transparent" />
                            </ControlTemplate>
                        </ContextMenu.Template>
                    </ContextMenu>
                </GridView.ColumnHeaderContextMenu>