Группировка ItemsControlby по свойству модели itemsource - PullRequest
0 голосов
/ 14 ноября 2018

С этим классом:

Public Class PageBetModel
Private _name As String

Public Property Name As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
    End Set
End Property

Private _group As String

Public Property Group As String
    Get
        Return _group
    End Get
    Set(ByVal value As String)
        _group = value
    End Set
End Property
End Class

Я хочу создать стиль с ItemsControl, нарисовать свойство Name и сгруппировать по свойству Group.

<ItemsControl ItemsSource="{Binding Path=Model}" ItemsPanel="{DynamicResource MyPanel}" ItemTemplate="{DynamicResource MyTemplate}"/>

<DataTemplate x:Key="MyTemplate">
    <Border MinHeight="100" BorderThickness="0,0,0,2" BorderBrush="#dfe1e0">

            <TextBlock x:Name="RadioButtonText" Margin="16,40,16,16" Width="287" Text="{Binding Path=Name}" FontFamily="Arial" FontSize="17" Foreground="#474747" FontWeight="SemiBold" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left"/>

    </Border>
</DataTemplate>

Я хочу отобразить этот простой дизайн, но сгруппировать разные имена с Expander по свойству Group.

1 Ответ

0 голосов
/ 14 ноября 2018

Установите ItemsSource для сгруппированного CollectionViewSource:

<CollectionViewSource x:Key="cvs" Source="{Binding Path=Model}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="Group" />
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

... и определите GroupStyle, который содержит Expander:

<ItemsControl ItemsPanel="{DynamicResource MyPanel}"
            ItemsSource="{Binding Source={StaticResource cvs}}"
            ItemTemplate="{DynamicResource MyTemplate}">
    <ItemsControl.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Expander IsExpanded="True">
                                    <Expander.Header>
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ItemsControl.GroupStyle>
</ItemsControl>
...