【问题标题】:Datagrid expander (from grouping) header数据网格扩展器(来自分组)标题
【发布时间】:2012-04-06 04:59:25
【问题描述】:

按照this 教程,我想到了在扩展器标题中放入更多数据。 我有 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 中显示更多数据?
  • 什么数据?你需要更具体。
  • “我有 2 个表格(文档 1 - * 条目)”。我正在显示按文档分组的所有条目。 dgEntries.Itemssource = Xmodel.Entry; .如果我在我的数据网格中放置一个 DataGridTextColumn 数据会重复。所以我想将带有文档信息的列放在扩展器标题中。

标签: wpf entity-framework datagrid grouping expander


【解决方案1】:

你可以这样做:

<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

【讨论】:

    猜你喜欢
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多