2016-07-04 20 views
0

我有一個數據網格與WPF應用程序中的分組數據。對於組頭,我有一個上下文菜單來刪除組。 datagrid的項目源與我的對象綁定。 如何檢測擴展頭的父/父組? 我試圖這樣做,但它返回null。如何獲取數據網格組擴展器標頭中的父節點

private void buttonDeleteHeader_Click(object sender, RoutedEventArgs e) 
{ 
    MenuItem menuItem = sender as MenuItem; 
    TextBlock header = menuItem.Parent as TextBlock; 
} 

UI的分組數據

m_infoList = new ObservableCollection<MyDataObject>(); 
m_infoList .Add(new MyDataObject 
     { ID = 1, Attr1 = "Level1", Attr2 = "Level2", Attr3 ="Level3", Attr4 = "Child1"}); 
m_infoList .Add(new MyDataObject 
     { ID = 2, Attr1 = "Level1", Attr2 = "Level2", Attr3 ="Level3", Attr4 = "Child2"}); 
CollectionView collectionView = (CollectionView)CollectionViewSource.GetDefaultView(m_infoList); 
      PropertyGroupDescription groupDescription1 = new PropertyGroupDescription("Attr1"); 
      PropertyGroupDescription groupDescription2 = new PropertyGroupDescription("Attr2"); 
      PropertyGroupDescription groupDescription3 = new PropertyGroupDescription("Attr3"); 
      collectionView.GroupDescriptions.Clear(); 
      collectionView.GroupDescriptions.Add(groupDescription1); 
      collectionView.GroupDescriptions.Add(groupDescription2); 
      collectionView.GroupDescriptions.Add(groupDescription3); 

UI

數據網格碼

<DataGrid x:Name="dataGrid" 
        ItemsSource="{Binding InfoList, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ....> 
    ... 
    <DataGrid.GroupStyle> 
       <GroupStyle> 
        <GroupStyle.ContainerStyle> 
         <Style TargetType="{x:Type GroupItem}"> 
          <Setter Property="Template"> 
           <Setter.Value> 
            <ControlTemplate> 
             <Expander x:Name="MyExpander" IsExpanded="True"> 
              <Expander.Header> 
               <StackPanel Orientation="Horizontal">      
                <TextBlock x:Name="MyExpanderHeader" Text="{Binding Name}" FontWeight="Bold" VerticalAlignment="Bottom" 
                   ToolTip="Right click to delete"> 
                 <TextBlock.ContextMenu> 
                  <ContextMenu> 
                   <MenuItem Header="Delete" Click="buttonDeleteHeader_Click"> 

                   </MenuItem> 
                  </ContextMenu> 
                 </TextBlock.ContextMenu> 
                </TextBlock> 
               </StackPanel> 
              </Expander.Header> 
              <ItemsPresenter Margin="20,0,0,0"/> 
             </Expander> 
            </ControlTemplate> 
           </Setter.Value> 
          </Setter> 
         </Style> 
        </GroupStyle.ContainerStyle> 
       </GroupStyle> 
      </DataGrid.GroupStyle> 
    </DataGrid> 
+0

將綁定設置爲空字符串不是更容易嗎?即Name = string.Empty – failedprogramming

+0

你的意思是在擴展器綁定? – user1850936

回答

1

這樣做:

MenuItem item = sender as MenuItem; 
ContextMenu ctxMenu = (ContextMenu) item.Parent; 

// ctxMenu.Parent will give you `Popup` 

TextBlock tb = (TextBlock) ctxMenu.PlacementTarget; 

遍歷的水平,你可以,如果你使用的是Binding使用RelativeSource及其AncestorLevel屬性,通過編程可以使用VisualTreeHelper類和穿越像這樣:

DependencyObject parent = VisualTreeHelper.GetParent(tb); 
while (parent.GetType() != typeof(Expander)) 
     parent = VisualTreeHelper.GetParent(parent); 

使用Snoop工具檢查視覺樹,並相應地遍歷。

+0

嗨,謝謝你的回答。它可以幫助我獲得所選擴展器的值。如果我想獲取其父頭的值,該怎麼辦?對於這種情況下,右鍵點擊3級,並希望得到2級和1級的價值.. – user1850936

+0

哦!非常感謝。現在我明白了視覺樹的概念。謝謝! – user1850936

相關問題