Unable to set the GroupStyle for a ListBox via Style?

.net, c#, wpf

Solution

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!

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

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.

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())

Problem

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> ``` 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)

Original source