如何在wpf中的组标题中显示属性名称

发布于 2024-12-08 17:46:30 字数 1265 浏览 0 评论 0原文

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

在上面的代码中,我希望显示其分组所依据的属性名称。 例如性别-->男生 ;性别-->女孩。

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

我应该提供什么?????????????在上面的xaml中?

另外,如果有任何好的书籍或链接可以解释 ListCollectionView 中分组的内部细节,请告诉我。

<!-- 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2024-12-15 17:46:30

DataContext 应该是 CollectionViewGroup,它将其 Name 属性设置为“group”(即 Gender 的值)。但是 CollectionViewGroup 和它来自哪个属性之间没有关联。从技术上讲,该属性可能有多个层次。

唯一好的选择是对另一个拥有您所需的所有信息的房产进行排序。 CollectionViewGroup.Name 属性是一个对象,而不是字符串。所以你可以这样做:

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; }
    }

}

然后像这样访问它:

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

这是基本想法,但你可以使 GenderGroup 更通用。因此,您可以传递具有参数的参数,而不是硬编码“Gender”。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文