2012-02-02 64 views
0

我有這樣一個CustomControl:如何在GridCell中使用ContextMenu?

 <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> 

在代碼隱藏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 
       } 

我從conrol在DataGrid電池用......當我點擊我的按鈕,我得到一個錯誤在這條線

b.ContextMenu.IsOpen = true; 

「文本菜單」不能有邏輯或視覺父

我怎樣才能解決這個erorr

+0

你爲什麼這樣做: b.ContextMenu.PlacementTarget = B ; b.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom; ContextMenuService.SetPlacement(b,System.Windows.Controls.Primitives.PlacementMode.Bottom); – Amit 2012-02-02 09:22:15

回答

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> 
相關問題