C# 如何获取数据网格组扩展头中的父节点
我有一个在WPF应用程序中包含分组数据的datagrid。对于组标题,我有一个上下文菜单来删除组。datagrid的项源与my对象绑定。 如何检测扩展器头的父/父组? 我尝试过这样做,但它返回nullC# 如何获取数据网格组扩展头中的父节点,c#,wpf,datagrid,header,expander,C#,Wpf,Datagrid,Header,Expander,我有一个在WPF应用程序中包含分组数据的datagrid。对于组标题,我有一个上下文菜单来删除组。datagrid的项源与my对象绑定。 如何检测扩展器头的父/父组? 我尝试过这样做,但它返回null private void buttonDeleteHeader_Click(object sender, RoutedEventArgs e) { MenuItem menuItem = sender as MenuItem; TextBlock header = menuItem
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);
m_infoList=newobserveCollection();
添加(新的MyDataObject)
{ID=1,Attr1=“Level1”,Attr2=“Level2”,Attr3=“Level3”,Attr4=“Child1”});
添加(新的MyDataObject)
{ID=2,Attr1=“Level1”,Attr2=“Level2”,Attr3=“Level3”,Attr4=“Child2”});
CollectionView CollectionView=(CollectionView)CollectionViewSource.GetDefaultView(m_信息列表);
PropertyGroupDescription groupDescription1=新的PropertyGroupDescription(“属性1”);
PropertyGroupDescription groupDescription2=新的PropertyGroupDescription(“属性2”);
PropertyGroupDescription groupDescription3=新的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>
...
执行以下操作:
MenuItem item = sender as MenuItem;
ContextMenu ctxMenu = (ContextMenu) item.Parent;
// ctxMenu.Parent will give you `Popup`
TextBlock tb = (TextBlock) ctxMenu.PlacementTarget;
要遍历这些级别,如果使用的是绑定
,则可以使用相对资源
及其AnteStorLevel
属性,并且可以通过编程方式使用VisualTreeHelper
类并向上遍历,如下所示:
DependencyObject parent = VisualTreeHelper.GetParent(tb);
while (parent.GetType() != typeof(Expander))
parent = VisualTreeHelper.GetParent(parent);
使用Snoop工具检查可视树,并进行相应的遍历。执行以下操作:
MenuItem item = sender as MenuItem;
ContextMenu ctxMenu = (ContextMenu) item.Parent;
// ctxMenu.Parent will give you `Popup`
TextBlock tb = (TextBlock) ctxMenu.PlacementTarget;
要遍历这些级别,如果使用的是绑定
,则可以使用相对资源
及其AnteStorLevel
属性,并且可以通过编程方式使用VisualTreeHelper
类并向上遍历,如下所示:
DependencyObject parent = VisualTreeHelper.GetParent(tb);
while (parent.GetType() != typeof(Expander))
parent = VisualTreeHelper.GetParent(parent);
使用Snoop工具检查可视树,并进行相应的遍历。将绑定设置为空字符串不是更容易吗?i、 e.Name=string.Emptyyou是指在expander绑定中?将绑定设置为空字符串不是更容易吗?i、 e.Name=string.Emptyyou是指在扩展器绑定中?您好,谢谢您的回答。它帮助我获得所选扩展器的值。如果我想获取其父标题的值,该怎么办?在这种情况下,右键单击level 3并希望获得level 2和level 1的值..哦!非常感谢。现在,我清楚了视觉树的概念。谢谢嗨,谢谢你的回答。它帮助我获得所选扩展器的值。如果我想获取其父标题的值,该怎么办?在这种情况下,右键单击level 3并希望获得level 2和level 1的值..哦!非常感谢。现在,我清楚了视觉树的概念。谢谢