Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display custom text in a GroupItem?

The typical ListView GroupStyle with an Expander looks roughly like this:

<ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander>
                                <Expander.Header>
                                    <TextBlock Text="{Binding Path=Name}" />
                                </Expander.Header>
                                <Expander.Content>
                                    <ItemsPresenter />
                                </Expander.Content>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</ListView.GroupStyle>

The TextBlock is bound to Name, which seems to produce the value of the GroupItem's object whose propertyName was passed into the PropertyGroupDescription which was added to a ListCollectionView's GroupDescriptions Collection (whew):

class MyClass
{
    public int Id { get; set; }
    public int FullName { get; set; }
    public string GroupName { get; set; } // used to group MyClass objects
}

MyView.GroupDescriptions.Add(new PropertyGroupDescription("GroupName"));

How can I get my ListView's group heading to display a property other than GroupName? The point I'm making is that GroupName is useful to split my objects into groups, but is not necessarily the name I wish to use for the headers of said groups.

like image 998
epalm Avatar asked Dec 26 '22 11:12

epalm


1 Answers

CollectionViewGroup.Name Property is of type object. This means your GroupName property doesn't have to be a string. Try something like this

class MyClass
{
    ...
    public MyGroup GroupName { get; set; } // used to group MyClass objects
}

class MyGroup
{
    public string HeaderText { get; set; }
    // maybe you will need an additional property to make this object
    // unique for grouping (e.g. for different groups with same HeaderText)
}

.

<Expander.Header>
    <TextBlock Text="{Binding Path=Name.HeaderText}" />
</Expander.Header>
like image 183
LPL Avatar answered Jan 12 '23 02:01

LPL