无法通过样式设置ListBox的GroupStyle? [英] Unable to set the GroupStyle for a ListBox via Style?

查看:34
本文介绍了无法通过样式设置ListBox的GroupStyle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一种样式,该样式将为我的ListBox控件设置GroupStyle属性,但是出现编译时错误:

I'm trying to create a Style that will set the GroupStyle property for my ListBox controls however I am getting a compile time error:

The Property Setter 'GroupStyle' cannot be set because it does not have an accessible set accessor. 

我的样式设置器看起来像这样:

My Style setter looks like this:

        <Setter Property="ListBox.GroupStyle">
            <Setter.Value>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}" />
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </Setter.Value>
        </Setter>

是否有解决此问题的方法,而且,如果没有针对此属性的设置器,那么我们如何能够首先在XAML中使用属性设置器语法对其进行内联定义?(对WPF还是新手)

Is there a work-around for this, and also, if there is no setter for this property, then how are we able to use property-setter syntax for it in XAML to define it inline, in the first place? (still new to WPF)

推荐答案

我刚刚想出了答案-这是因为XAML编译器根据映射到XML属性的类型来处理元素标记之间的任何内容.我刚刚记得的内容!

I just figured out the answer - it's because of the way the XAML compiler treats any content between the element tags, based on the type of the property mapped to the content I just remembered!

如果该属性是ContentControl,则您在两个标记之间定义的元素将分配给该Content属性,但是,如果该元素是IList的实例(GroupStyle是),则.NET实际上会调用.在封面下添加()

If the property is a ContentControl, then the element you define between two tags gets assigned to that Content property, however, if the element is an instance of an IList (which is what GroupStyle is), then .NET actually calls .Add() underneath the covers

在这种情况下,GroupStyle实际上是一个ObservableCollection,因此是一个IList,因此我们实际上并未分配给GroupStyle对象,而是将其添加到了集合中.

In this case, the GroupStyle is actually an ObservableCollection and hence an IList, therefore we are not actually assigning to the GroupStyle object, we are ADDING to the collection.

换句话说,元素标记之间由内容(通过控件的ContentProperty属性映射)表示的属性的类型会影响XAML编译器对其进行解释(直接分配或调用.Add())的方式

In otherwords, the type of the property that is represented by content (mapped via the ContentProperty attribute of a control) in between the element tags influences the way the XAML compiler interprets it (direct assignment or calling .Add())

这篇关于无法通过样式设置ListBox的GroupStyle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆