【问题标题】:WPF ListView inside Grouped TreeView not working分组树视图中的 WPF ListView 不起作用
【发布时间】:2017-02-24 16:50:43
【问题描述】:

在 WPF 项目中,我有一组 record 对象,其属性为 SchoolSubjectFirstNameLastName。记录由SchoolSubject 在XAML 中使用CollectionViewSource 分组,TreeView 使用它来显示分组的项目。分组工作正常。

问题是我想显示ListViewSubject 下记录的FirstNameLastName,使用GridView 作为其视图,FirstNameLastName 作为列,但我不知道该怎么做。

这是当前显示的图像:

这里有一些示例代码来说明我的意思。

public class Record
{
    public string School { get; set; }
    public string  Subject { get; set; }
    public  string FirstName { get; set; }
    public string LastName { get; set; }
}

后面的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();


        var records = new Record[]
        {
            new Record() { School = "School A", Subject = "Maths" , FirstName = "Fred" , LastName = "Blogs"},
            new Record() { School = "School A", Subject = "English" , FirstName = "Alice" , LastName = "Lane"},
            new Record() { School = "School B", Subject = "Geography" , FirstName = "John" , LastName = "Smith"},
            new Record() { School = "School B", Subject = "Geography" , FirstName = "Burt" , LastName = "Lancaster"},
            new Record() { School = "School C", Subject = "Chemistry" , FirstName = "Dee" , LastName = "Kaye"}

        };

        this.DataContext = records;
    }
}

XAML:

<Window x:Class="ListViewInTreeView.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ListViewInTreeView"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <CollectionViewSource x:Key="listings"                   
                  Source="{Binding .}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="School" />
            <PropertyGroupDescription PropertyName="Subject" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>

<Grid>

    <TreeView Grid.Row="4" DataContext="{StaticResource listings}"  ItemsSource="{Binding}" >

        <TreeView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,5"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander Margin="0,0,0,0" IsExpanded="True" BorderBrush="#FFA4B97F" 
                                    BorderThickness="0,0,0,1">
                                        <Expander.Header>
                                            <DockPanel>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>
                                                <TextBlock Text=" : "/>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                                <TextBlock Text=" items(s)"/>
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter Margin="20,0,0,0" />
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </TreeView.GroupStyle>

        <TreeView.Resources>

            <!-- NEED HELP HERE I THINK ?-->
            <HierarchicalDataTemplate DataType="{x:Type GroupItem}">
                <ListView ItemsSource="{Binding .}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
                            <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
                        </GridView>
                    </ListView.View>
                </ListView>
            </HierarchicalDataTemplate>

        </TreeView.Resources>
    </TreeView>

</Grid>
</Window>

【问题讨论】:

    标签: c# wpf xaml treeview


    【解决方案1】:

    使用 2 个GroupStyles,一个用于School 组,另一个用于Subject 组:

    <TreeView Grid.Row="4" DataContext="{StaticResource listings}"  ItemsSource="{Binding}" >
        <TreeView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,5"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander Margin="0,0,0,0" IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1">
                                        <Expander.Header>
                                            <DockPanel>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>
                                                <TextBlock Text=" : "/>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                                <TextBlock Text=" items(s)"/>
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter Margin="20,0,0,0" />
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,5"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander Margin="0,0,0,0" IsExpanded="True" BorderBrush="#FFA4B97F"  BorderThickness="0,0,0,1">
                                        <Expander.Header>
                                            <DockPanel>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>
                                                <TextBlock Text=" : "/>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                                <TextBlock Text=" items(s)"/>
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ListView ItemsSource="{Binding Items}">
                                                <ListView.View>
                                                    <GridView>
                                                        <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
                                                        <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
                                                    </GridView>
                                                </ListView.View>
                                            </ListView>
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </TreeView.GroupStyle>
    </TreeView>
    

    您可能还想将ExpanderGroupItem 的通用属性设置移动到GroupStyles 都使用的两种样式中:

    <TreeView Grid.Row="4" DataContext="{StaticResource listings}"  ItemsSource="{Binding}" >
        <TreeView.Resources>
            <Style TargetType="GroupItem">
                <Setter Property="Margin" Value="0,0,0,5"/>
            </Style>
            <Style TargetType="Expander">
                <Setter Property="Margin" Value="0,0,0,0" />
                <Setter Property="IsExpanded" Value="True" />
                <Setter Property="BorderBrush" Value="#FFA4B97F" />
                <Setter Property="BorderThickness" Value="0,0,0,1" />
                <Setter Property="Header" Value="{Binding}" />
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <DockPanel>
                                <TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>
                                <TextBlock Text=" : "/>
                                <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                <TextBlock Text=" items(s)"/>
                            </DockPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.Resources>
        <TreeView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander>
                                        <Expander.Content>
                                            <ItemsPresenter Margin="20,0,0,0" />
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander>
                                        <Expander.Content>
                                            <ListView ItemsSource="{Binding Items}">
                                                <ListView.View>
                                                    <GridView>
                                                        <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
                                                        <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
                                                    </GridView>
                                                </ListView.View>
                                            </ListView>
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </TreeView.GroupStyle>
    </TreeView>
    

    【讨论】:

    • 嗨@mm8,效果很好!谢谢!。但是不知道为什么。不明白第二个Style 是如何被正确提取的,因为它们的目标都比GroupItem 更具体?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    相关资源
    最近更新 更多