【问题标题】:Unable to set the GroupStyle for a ListBox via Style?无法通过 Style 为 ListBox 设置 GroupStyle?
【发布时间】:2012-08-18 21:57:49
【问题描述】:

我正在尝试创建一个 Style 来为我的 ListBox 控件设置 GroupStyle 属性,但是我收到了编译时错误:

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

我的样式设置器如下所示:

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

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

【问题讨论】:

    标签: c# .net wpf


    【解决方案1】:

    我刚刚找到答案 - 这是因为 XAML 编译器根据映射到我刚刚记住的内容的属性类型来处理元素标记之间的任何内容的方式!

    如果属性是 ContentControl,那么您在两个标记之间定义的元素将分配给该 Content 属性,但是,如果该元素是 IList 的实例(这就是 GroupStyle),那么 .NET 实际上会调用 .幕后的 Add()

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

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

    【讨论】:

    • 解决方案的任何代码示例?
    【解决方案2】:


    你可以如下:

            <ListBox.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource listContainerStyle}"/>
            </ListBox.GroupStyle>
    


    <Style x:Key="listContainerStyle" TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Expander Header="{Binding Name}" IsExpanded="True">
                        <ItemsPresenter />
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    Avi.

    【讨论】:

      【解决方案3】:
      //set you datatemplate as a resource
      <DataTemplate x:Key="categoryTemplate">
      <TextBlock Text="{Binding Path=Name}"/>
      </DataTemplate>
      
      
      //set header template binding to staticresource
      <ListBox Name="lst"> 
         <ListBox.GroupStyle>
            <GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" />
          </ListBox.GroupStyle>
      </ListBox>
      

      【讨论】:

      • 这行得通,谢谢!然而,从 XAML 的角度来看,为什么我们可以在属性没有设置器的情况下使用属性设置器语法定义一个带有空白 的内联 GroupStyle?我对此感到困惑。
      • 如果你也能回答最后一个问题,我会将你的答案标记为解决方案:)
      • 这是因为 GroupStyle 没有 setter public ObservableCollection GroupStyle { get; } (msdn msdn.microsoft.com/en-us/library/…)
      • 是的,我知道(我什至提到它没有属性设置器) - 那么为什么 XAML 的属性设置器语法有效?
      • 是的,但是当您指定 GroupStyle 项目时,您只能通过属性 GroupStyle (containstyle,headertemplate...) 设置值,它只是一个获取访问权限而不是
      【解决方案4】:

      为了更好的理解 您可以在代码中添加 groupstyle(这是 XAML 所做的)

      GroupStyle g = new GroupStyle();
      ListBox ls = new ListBox();
      ls.GroupStyle.Add(g);
      

      但不能设置 GroupStyle

      GroupStyle g = new GroupStyle();
      ListBox ls = new ListBox();
      ls.GroupStyle=g;//error because GroupStyle has only a getter
      

      【讨论】:

      • 混淆是因为 ListBox.GroupStyle 属性是 GroupStyle 的容器
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      相关资源
      最近更新 更多