根据itemsource模型的属性对ItemsControl进行分组

时间:2018-11-14 09:23:56

标签: wpf vb.net datatemplate itemscontrol

使用此类:

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 :(得分:1)

ItemsSource设置为分组的CollectionViewSource

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

...并定义包含GroupStyle的{​​{1}}:

Expander