我有一個數據網格與WPF應用程序中的分組數據。對於組頭,我有一個上下文菜單來刪除組。 datagrid的項目源與我的對象綁定。 如何檢測擴展頭的父/父組? 我試圖這樣做,但它返回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);
數據網格碼
<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>
將綁定設置爲空字符串不是更容易嗎?即Name = string.Empty – failedprogramming
你的意思是在擴展器綁定? – user1850936