How do I display the property name in the group header in wpf
Asked Answered
T

1

6
<!-- GroupHeaderStyle -->
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander IsExpanded="False" Margin="15,0,0,0">
                    <Expander.Header>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding ????????????}"/>
                            <TextBlock Text="-->"/>
                            <TextBlock Text="{Binding Name}"/>
                        </StackPanel>
                    </Expander.Header>
                    <ItemsPresenter />
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

In the above code I wish to display the property name by which it is grouped. E.g. Gender --> Boy ; Gender --> Girl.

public class Test
{
    string gender;
    public string Gender
    {
        get { return gender; }
        set { gender = value; }
    } 
}

What should i provide for ???????????? in the above xaml?

Also, please let me know if there is any good book or link which explains the internal details of grouping in ListCollectionView.

Turtle answered 6/10, 2011 at 11:10 Comment(0)
S
3

The DataContext should be an instance of CollectionViewGroup, which will have it's Name property set to the "group" (i.e. the value of Gender). But there is no correlation between the CollectionViewGroup and what property it came from. Technically, the property could be multiple levels deep.

The only good alternative is to sort on another property that has all the information you need. The CollectionViewGroup.Name property is an object, not a string. So you could do something like:

public class GenderGroup {

    public GenderGroup(string gender) {
        this.Gender = gender;
    }

    public string Gender { get; private set; }
    public string Title {
        get {
            return "Gender";
        }
    }

    public override bool Equals(object obj) {
        var other = obj as GenderGroup;
        return (other != null && string.Equals(this.Gender, other.Gender));
    }
}

public class Test {
    string gender;
    GenderGroup genderGroup = new GenderGroup("none");

    public string Gender {
        get { return gender; }
        set {
            gender = value;
            genderGroup = new GenderGroup(gender);
        }
    } 

    public GenderGroup GenderGroup {
        get { return genderGroup; }
    }

}

And then access it like this:

<!-- GroupHeaderStyle -->
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander IsExpanded="False" Margin="15,0,0,0">
                    <Expander.Header>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name.Title}"/>
                            <TextBlock Text="-->"/>
                            <TextBlock Text="{Binding Name.Gender}"/>
                        </StackPanel>
                    </Expander.Header>
                    <ItemsPresenter />
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

That's the basic idea, but you could make GenderGroup more generic. So instead of hard-coding "Gender" you could pass that has a parameter.

Sledgehammer answered 6/10, 2011 at 17:26 Comment(2)
Thank you CodeNaked for the reply... Does that mean i can't get the Property name displayed in the header with any straight forward way...?Turtle
@SudhakarSingh - I added the only work around I can think of to my answer.Sledgehammer

© 2022 - 2024 — McMap. All rights reserved.