我有一个像这样的CustomControl:
<Button Name="b" Height="20" Click="b_Click" Content="operation" Width="60">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Copy"></MenuItem>
<MenuItem Header="cut"></MenuItem>
<MenuItem Header="delete"></MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
CodeBehind ia中的:
if (b.ContextMenu != null && b.ContextMenu.IsOpen == false)
{
b.ContextMenu.PlacementTarget = b;
b.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
ContextMenuService.SetPlacement(b, System.Windows.Controls.Primitives.PlacementMode.Bottom);
b.ContextMenu.IsOpen = true;//I have error in this Line
}
我在我的数据网格单元格中使用...当我点击我的按钮时,我在此行中出现错误
b.ContextMenu.IsOpen = true;
“ContextMenu”不能具有逻辑或可视父级
我该如何解决这个错误
答案 0 :(得分:0)
工作应用的示例,希望这能为您提供所需的线索......
<DataGrid Margin="0,0,195,72" x:Name="A">
<DataGrid.Resources>
<ContextMenu x:Key="ContextMenu">
<MenuItem Header="Copy" Click="MenuItem_Click"></MenuItem>
<MenuItem Header="cut" Click="MenuItem_Click"></MenuItem>
<MenuItem Header="delete" Click="MenuItem_Click"></MenuItem>
</ContextMenu>
</DataGrid.Resources>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="ContextMenu" Value="{StaticResource ContextMenu}">
</Setter>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="A" Binding="{Binding}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>