2017-02-24 68 views
2

在WPF项目中,我有一组record对象,其属性为School,Subject,FirstNameLastName。这些记录按SchoolSubject分组,使用XAML中的CollectionViewSource,并由TreeView用于显示分组项目。分组工作正常。问题是我想显示FirstNameLastNameSubject下的记录在ListView使用GridView作为其查看与FirstNameLastName列,但我不知道如何做到这一点?WPF:分组TreeView内部的ListView不工作

许多在此先感谢您的帮助。

这是当前显示的图像:

enter image description here

下面是一些示例代码,以显示我的意思。

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> 

回答

1

使用2个GroupStyle S,一个用于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移动共同财产设置由两个GroupStyle小号使用的两种风格:

<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> 

enter image description here

+0

嗨@ MM8,它的伟大工程!谢谢!。不知道为什么。不明白第二个'Style'是否被正确拾取,因为它们都没有指定比GroupItem更具体的特征? – Cleve