簡體   English   中英

根據項目屬性的列表框分隔線

[英]ListBox Divider according to Item Property

我有一個ViewLookupItem類型的項目列表,它具有以下屬性:

顯示不帶組的LookupItems的屬性

例如,這個項目示例沒有組,但是一個項目可以屬於一個組,如下所示:

與組一起顯示LookupItems的屬性

我想做的是,當某個項目屬於一個組時,我想在分隔線下顯示它,如果該項目不屬於某個組,則正常顯示它,這是我當前的ListBox (在左側):

列表框

當項目屬於一個組時,我希望它看起來像這樣:

分組列表框

我想我需要使用某種觸發器,但我不知道如何使用它們,任何幫助將不勝感激:)這是我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>

您可以在您的收藏集上創建一個收藏夾視圖,並告訴它按“ Group屬性Group 然后,您可以在ListBox上定義GroupStyle來定義如何可視化分組。

這是一個簡單的示例(不遵循最佳實踐或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>

結果

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM