如何访问未填充单元格的DataGrid区域?
我想仅为此区域添加上下文菜单
答案 0 :(得分:1)
如果没有为行指定不同的上下文菜单(或者“DataGrid.RowStyle”属性)。
“解决方法”是简单地为行指定不同的上下文菜单。
<DataGrid>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Menu Item in Blank Area" />
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Menu Item just for Populated Rows" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
答案 1 :(得分:1)
根据您对上一个答案的评论,我发现您要在标题上设置一个ContextMenu,并为您的单元格和空格设置一个单独的ContextMenu。我可以通过覆盖模板来设置单独的ContextMenus:
<Window x:Class="Apc_System_Generator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<ResourceDictionary>
<ContextMenu x:Key="cmenu">
<MenuItem Header="FOO" />
<MenuItem Header="BAR" />
</ContextMenu>
<ContextMenu x:Key="omenu">
<MenuItem Header="LOL" />
<MenuItem Header="WUT" />
</ContextMenu>
</ResourceDictionary>
</Grid.Resources>
<DataGrid Name="LOL" AutoGenerateColumns="True" ContextMenu="{StaticResource cmenu}">
<DataGrid.Template>
<ControlTemplate TargetType="{x:Type DataGrid}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DataGridColumnHeadersPresenter Grid.Row="0" ContextMenu="{StaticResource omenu}" />
<DataGridRowsPresenter Background="gray" Grid.Row="1" IsItemsHost="True" />
</Grid>
</ControlTemplate>
</DataGrid.Template>
</DataGrid>
</Grid>
</Window>