根据Item属性的ListBox分隔符

时间:2012-07-05 11:19:33

标签: c# .net wpf xaml wpf-controls

我有一个ViewLookupItem类型的项目列表,其中包含以下属性:

Show Properties of LookupItems without Group

例如,此项目示例没有组,但是项目可以属于某个组,如下所示:

Show Properties of LookupItems with Group

我想要做的是,当一个项目属于一个组我希望在分隔符下显示它时如果该项目不属于一个组正常显示它,这是我当前的ListBox下面的({左边的那个):

listbox

当项目属于某个组时,我希望它看起来像这样:

grouped list box

我想我需要使用某种触发器,但我不知道如何使用它们,任何帮助都会非常感激:)这是我ListBox的当前XAML:

<ListBox BorderBrush="Black" 
         Height="303" 
         HorizontalAlignment="Left" 
         Margin="15,59,0,0" 
         Name="lstResults" 
         VerticalAlignment="Top" 
         Width="295" 
         SelectionMode="Multiple" 
         AllowDrop="True" 
         local:ListBoxSelector.Enabled="True"     
         PreviewMouseLeftButtonDown="lstResults_PreviewMouseLeftButtonDown" 
         PreviewMouseMove="lstResults_PreviewMouseMove" TabIndex="1" 
         PreviewMouseDoubleClick="lstResults_PreviewMouseDoubleClick">
    <ListBox.ItemTemplate>
        <DataTemplate>                
            <TextBlock Text="{Binding TextValue}" 
                       TextWrapping="Wrap" 
                       FontSize="14"/>                    
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

1 个答案:

答案 0 :(得分:0)

您可以在集合上创建集合视图,并告诉它按Group属性进行分组。然后,您可以在GroupStyle上定义ListBox,以定义分组的可视化方式。

这是一个简单的示例(不遵循最佳实践或MVVM或任何内容,但用于说明您的方案的集合视图):

<强> MainWindow.xaml.cs

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;

namespace SO11343251
{
    public partial class MainWindow : Window
    {
        private readonly ICollection<ViewLookupItem> items;
        private readonly ICollectionView itemsView;

        public MainWindow()
        {
            InitializeComponent();

            this.items = new List<ViewLookupItem>();

            this.items.Add(new ViewLookupItem("Amenities", "No Group"));
            this.items.Add(new ViewLookupItem("Indoor", "Area"));
            this.items.Add(new ViewLookupItem("Outdoor", "Area"));

            this.itemsView = new ListCollectionView((IList)this.items);
            this.itemsView.GroupDescriptions.Add(new PropertyGroupDescription("Group"));

            this.DataContext = this;
        }

        public ICollection<ViewLookupItem> Items
        {
            get { return this.items; }
        }

        public ICollectionView ItemsView
        {
            get { return this.itemsView; }
        }
    }

    public class ViewLookupItem
    {
        private readonly string name;
        private readonly string group;

        public ViewLookupItem(string name, string group)
        {
            this.name = name;
            this.group = group;
        }

        public string Name
        {
            get { return this.name; }
        }

        public string Group
        {
            get { return this.group; }
        }
    }
}

<强> MainWindow.xaml

<Window x:Class="SO11343251.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <ListBox ItemsSource="{Binding ItemsView}">
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="textBlock" FontWeight="Bold" Text="{Binding Name}"/>

                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Name}" Value="No Group">
                                <Setter TargetName="textBlock" Property="Visibility" Value="Collapsed"/>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListBox.GroupStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>

<强>结果

enter image description here