WPF数据网格显示多种类型

5
什么是在WPF数据网格中显示多种类型数据的最佳方法?例如类别和产品: | 产品名称 | 描述 | 价格 | --------------------------------------- IT - 类别 --------------------------------------- 监视器 - 类别 --------------------------------------- | 监视器1 | ... | 100美元 | | 监视器2 | ... | 99美元 | | 监视器3 | ... | 120美元 | --------------------------------------- 硬盘驱动器(HDD) - 类别 --------------------------------------- | Hdd 1 | ... | 50美元 | | Hdd 2 | ... | 45美元 | --------------------------------------- 电子产品类别 --------------------------------------- ...
我想在顶部显示产品列,并更改类别的模板。我知道有一个单元格模板选择器,但是否有一种方式可以为整个行指定模板选择器? 谢谢。

1
看起来你的数据是分层的,如果是这样的话,你可能想使用更适合的控件,比如 TreeView - Adi Lester
1个回答

6
如果您为数据创建了一个CollectionViewsource,那么可以在集合GroupDescriptions中使用PropertyGroupDescription将所有数据按照所需属性进行分组。
然后在DataGrid中,您可以创建一个GroupStyle来显示TextBlock或其他内容,以便将所有组在DataGrid中分开显示。
以下是完整的演示示例,因为这样更容易展示而不是解释:)
Xaml:
<Window x:Class="WpfApplication20.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="505" Width="525" Name="UI">

    <Window.Resources>

        <!--Create CollectionViewSource and set the property you want to group by-->
        <CollectionViewSource x:Key="MyItems" Source="{Binding Items, ElementName=UI}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Category" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>

    <Grid>
        <DataGrid ItemsSource="{Binding Source={StaticResource MyItems}}">
            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <!--Name property is the GroupName created by the CollectionViewSource-->
                                <TextBlock Text="{Binding Path=Name}" Margin="10,0,0,0" FontSize="18" FontWeight="Bold"/>
                            </StackPanel>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </DataGrid.GroupStyle>
        </DataGrid>
    </Grid>
</Window>

代码:

namespace WpfApplication20
{
    public partial class MainWindow : Window
    {
        private ObservableCollection<MyDataObject> _items = new ObservableCollection<MyDataObject>();

        public MainWindow()
        {
            InitializeComponent();
            Items.Add(new MyDataObject { Category = "IT", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
            Items.Add(new MyDataObject { Category = "Monitors", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
            Items.Add(new MyDataObject { Category = "Monitors", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
            Items.Add(new MyDataObject { Category = "Monitors", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
            Items.Add(new MyDataObject { Category = "HDD", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
            Items.Add(new MyDataObject { Category = "HDD", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
        }

        public ObservableCollection<MyDataObject> Items
        {
            get { return _items; }
            set { _items = value; }
        }
    }

    public class MyDataObject
    {
        public string Category { get; set; }
        public string ProductName { get; set; }
        public string Description { get; set; }
        public string Price { get; set; }
    }
}

结果:

输入图像描述

增强功能

DataGridContainerStyle中重写GroupItem模板以使用Expander可能是一个不错的想法,这样你就可以展开和折叠组。

例如:

    <DataGrid ItemsSource="{Binding Source={StaticResource MyItems}}" CanUserAddRows="False">
        <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander Name="expander">
                                        <Expander.Header>
                                            <StackPanel >
                                                <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
                                                <TextBlock Text="{Binding ItemCount, StringFormat={}Items: {0}}" FontSize="9" />
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>
    </DataGrid>

结果:

在此输入图片描述


(该图片未提供说明)

谢谢您的建议,但我不认为分组可以解决我的问题,因为我想将记录HDD和显示器作为IT的子级。为此,在我的ViewModel中,我将两种类型的对象:类别和产品合并到同一集合中。我的问题是,我希望能够为这两种类型的记录应用不同的模板。我知道我可以使用单元格模板选择器,但我想知道是否可以使用项模板选择器,以便合并所有类别记录的单元格。 - paccic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接