2016-07-04 7 views
0

WPFアプリケーションでグループ化されたデータを持つデータグリッドがあります。グループヘッダーについては、グループを削除するためのコンテキストメニューがあります。データグリッドのアイテムソースは、オブジェクトとバインドされています。 エクスパンダヘッダーの親/親グループを検出するにはどうすればよいですか? 私はこのようにしようとしましたが、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); 

は検査するスヌープツールを使用しますビジュアルツリーを作成し、それに応じてトラバースします。

+0

こんにちは、お返事ありがとうございます。選択したエキスパンダの値を取得するのに役立ちます。親ヘッダーの値を取得する場合はどうすればよいですか?この場合、レベル3を右クリックし、レベル2とレベル1の値を取得したいと考えています。 – user1850936

+0

ああ!本当にありがとう。今私はビジュアルツリーの概念ではっきりしています。ありがとう! – user1850936

関連する問題