0

ViewLookupItemこれらのプロパティを持つタイプのアイテムのリストがあります。

グループなしでLookupItemsのプロパティを表示する

たとえば、このアイテムの例にはグループがありませんが、アイテムは次のようにグループに属することができます。

グループでLookupItemsのプロパティを表示する

私がやりたいのは、アイテムがグループに属している場合、仕切りの下に表示したいのですが、アイテムがグループに属していない場合は、通常どおりに表示します。これは、ListBox以下の私の現在の値です(左側)。

リストボックス

アイテムがグループに属している場合は、次のようになります。

グループ化されたリストボックス

ある種のトリガーを使用する必要があると思いますが、それらの使用方法がわかりません。助けていただければ幸いです:)これは私の現在のXAMLですListBox

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

1 に答える 1

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>

結果

ここに画像の説明を入力してください

于 2012-07-05T12:03:18.777 に答える