GroupStyleSelector for ListView

I'm trying to create a custom groupstyle for my ListView as per other questions I've seen here on Stack Overflow.

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:

Error 1 'TestGroupStyleSelector': cannot be obtained from closed type 'System.Windows.Controls.GroupStyleSelector'

Error 2 An object of type 'TestGroupStyleSelector' cannot be applied to a property that type 'System.Windows.Controls.GroupStyleSelector' expects.

I have declared a class as other questions here have shown, I am quite lost at this point how to create a groupstyleselector for my list, any ideas?

+3


source to share


2 answers


GroupStyleSelector is the delegate exposed by the ItemsControl:

Using:

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

      

Deceleration:



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

      

It is a delegate of a type that cannot be inherited from do prior to language specifications.

what you need to do is create a class derived from StyleSelector:

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

      

+1


source


In WPF using

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

      

and inheriting your selector from GroupStyleSelector will result in a "cannot be obtained from private type" System.Windows.Controls.GroupStyleSelector exception.



Use instead

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

      

and inherits your selector from StyleSelector

+1


source







All Articles