簡體   English   中英

如何在數據網格組擴展器頭中獲取父節點

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

我在WPF應用程序中有一個帶有分組數據的datagrid。 對於組標題,我有一個上下文菜單刪除該組。 數據網格的項目源與我的對象綁定。 如何檢測擴展器標頭的父/母組? 我試圖這樣做,但它返回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>

做這個 :

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

// ctxMenu.Parent will give you `Popup` 

TextBlock tb = (TextBlock) ctxMenu.PlacementTarget;

要遍歷這些級別,可以使用RelativeSource及其AncestorLevel屬性(如果使用的是Binding ,並且可以以編程方式使用VisualTreeHelper類並像這樣向上遍歷:

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

使用探聽工具檢查視覺樹,然后進行相應遍歷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM