已关注 this教程 我有一个想法,可以在 Expander Header 中放入更多数据。 我有 2 个表(文档 1 - * 条目)。 我正在显示按文档分组的条目,并且我不希望某些数据在 datagrid 所以我想将它放在扩展器标题中。
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text=" - "/>
**<TextBlock Text="{Binding Path=Document.Number or Name2}"/>**
</StackPanel>
...
最佳答案
你可以这样做:
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupItem}}, Converter={StaticResource ResourceKey=groupToTitleConverter}}" />
</StackPanel> </Expander.Header>
转换器:
public class GroupToTitleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
GroupItem groupItem = value as GroupItem;
CollectionViewGroup collectionViewGroup = groupItem.Content as CollectionViewGroup;
EntryViewModel entryViewModel = collectionViewGroup.Items[0] as EntryViewModel;
string title = string.Format("{0} - {1} {2}", entryViewModel.Id, entryViewModel.Numar, entryViewModel.Obiect);
return title;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
从组中的集合中获取第一项来形成标题标题可能不是最优雅的解决方案,但它会达到目的。
完整代码可在此处获取:ExpanderHeadersInDataGridGroupStyle.zip
关于wpf - 数据网格扩展器(来自分组)标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9808619/