简体   繁体   English

ListView 的 GroupStyleSelector

[英]GroupStyleSelector for a ListView

I am trying to make a custom groupstyle sector for my ListView as per other questions I've seen here on stack overflow.我正在尝试根据我在堆栈溢出时看到的其他问题为我的 ListView 创建一个自定义组样式扇区。

public class TestGroupStyleSelector : GroupStyleSelector
{
    protected override GroupStyle SelectGroupStyleCore(object item, uint level)
    {
            return (GroupStyle)App.Current.Resources["grpStyle"];
    }
}

<ListView GroupStyleSelector="{StaticResource grpStyleSelector}">

I have two errors with this:我有两个错误:

Error 1 'TestGroupStyleSelector': cannot derive from sealed type 'System.Windows.Controls.GroupStyleSelector'错误 1“TestGroupStyleSelector”:无法从密封类型“System.Windows.Controls.GroupStyleSelector”派生

Error 2 An object of the type "TestGroupStyleSelector" cannot be applied to a property that expects the type "System.Windows.Controls.GroupStyleSelector".错误 2 “TestGroupStyleSelector”类型的对象不能应用于需要“System.Windows.Controls.GroupStyleSelector”类型的属性。

I have declared the class as other questions on here have shown, I am pretty lost at this point as to how to create a groupstyleselector for my listview, any ideas?我已经声明了这个类,因为这里的其他问题已经显示,此时我对如何为我的列表视图创建 groupstyleselector 感到非常迷茫,有什么想法吗?

In WPF, using在 WPF 中,使用

<ListView GroupStyleSelector="{StaticResource grpStyleSelector}" />

and inheriting your selector from GroupStyleSelector will result in a 'cannot derive from sealed type 'System.Windows.Controls.GroupStyleSelector' exception.并且从GroupStyleSelector继承您的选择器将导致“无法从密封类型‘System.Windows.Controls.GroupStyleSelector’派生”异常。

Instead, use相反,使用

<ListView>
   <ListView.GroupStyle>
     <GroupStyle ContainerStyleSelector="{StaticResource grpStyleSelector}"  />
   </ListView.GroupStyle>
</ListView>

and inherit your selector from StyleSelector并从StyleSelector继承您的选择器

GroupStyleSelector is a Delegate exposed by ItemsControl : GroupStyleSelectorItemsControl公开的Delegate

Usage:用法:

public GroupStyleSelector GroupStyleSelector
{
    get => (GroupStyleSelector)GetValue(GroupStyleSelectorProperty);
    set => SetValue(GroupStyleSelectorProperty, value);
}

Declaration:宣言:

public delegate GroupStyle GroupStyleSelector(CollectionViewGroup group, int level);

It is of type delegate which can't be inherited from as per the language specifications.根据语言规范,它属于不能继承的delegate类型。

What you need to do is create a class deriving from StyleSelector :您需要做的是创建一个派生自StyleSelector的类:

public class GroupStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {   
        return base.SelectStyle(item, container);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM