【问题标题】:How to get parent nodes in data grid group expander header如何在数据网格组扩展器标题中获取父节点
【发布时间】:2016-07-04 02:09:46
【问题描述】:

我在 WPF 应用程序中有一个包含分组数据的数据网格。对于组标题,我有一个上下文菜单来删除组。数据网格的项目源与我的对象绑定。 如何检测扩展头的父/父组? 我试过这样做,但它返回 null。

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

数据分组

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

用户界面

数据网格代码

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

【问题讨论】:

  • 将绑定设置为空字符串不是更容易吗?即名称 = string.Empty
  • 你的意思是在扩展器绑定中?

标签: c# wpf datagrid header expander


【解决方案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 工具检查可视化树,并进行相应的遍历。

【讨论】:

  • 您好,感谢您的回答。它可以帮助我获得所选扩展器的值。如果我想获取其父标头的值怎么办?对于这种情况,右键单击级别 3 并希望获得级别 2 和级别 1 的值..
  • 哦!非常感谢。现在我对视觉树的概念很清楚了。谢谢!
猜你喜欢
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
相关资源
最近更新 更多