简体   繁体   English

如何在数据网格组扩展器头中获取父节点

[英]How to get parent nodes in data grid group expander header

I have a datagrid with grouped data in a WPF application. 我在WPF应用程序中有一个带有分组数据的datagrid。 For the group header, I have a context menu to delete the group. 对于组标题,我有一个上下文菜单删除该组。 The item source of the datagrid is bind with my object. 数据网格的项目源与我的对象绑定。 How to detect the parent/ parents group of the expander header? 如何检测扩展器标头的父/母组? I have tried to do like this but it returns null. 我试图这样做,但它返回null。

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

Grouping of Data 数据分组

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 用户界面

用户界面

Data Grid codes 数据网格代码

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

Do this : 做这个 :

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

// ctxMenu.Parent will give you `Popup` 

TextBlock tb = (TextBlock) ctxMenu.PlacementTarget;

To traverse the levels, you can use RelativeSource and its AncestorLevel property if you are using Binding , and programmatically you can use VisualTreeHelper class and traverse up like this : 要遍历这些级别,可以使用RelativeSource及其AncestorLevel属性(如果使用的是Binding ,并且可以以编程方式使用VisualTreeHelper类并像这样向上遍历:

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

Use Snoop tool to inspect the visual tree, and traverse accordingly. 使用探听工具检查视觉树,然后进行相应遍历。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM