I'm looking for the best way to update a GroupStyle .
CS :
I Have a GroupStyleSelector (Simplified) :
public class CountBasedGroupStyleSelector : StyleSelector
{
public Style SingleItemGroupStyle { get; set; }
public Style MultipleItemGroupStyle { get; set; }
public override Style SelectStyle(object item, DependencyObject container)
{
var group = item as CollectionViewGroup;
return group.ItemCount == 1 ? SingleItemGroupStyle : MultipleItemGroupStyle;
}
}
XAML :
<tsk:CountBasedGroupStyleSelector x:Key="groupStyleSelector" SingleItemGroupStyle="{StaticResource SingleItemGroupStyle}"
MultipleItemGroupStyle="{StaticResource MultipleItemGroupStyle}" />
<TreeView>
<TreeView.GroupStyle>
<GroupStyle x:Name="groupStyle" ContainerStyleSelector="{StaticResource groupStyleSelector}" />
</TreeView.GroupStyle>
</TreeView>
I wan't to trigger the GroupStyleSelector's selection again when something changes , for example when an item is removed or add from a group .
any idea's how this can be done ?
FYI , i thought of rigging something up using an AttachedProperty Bound on each GroupItem
GroupItemStyles : (Simplified)
<Style x:Key="MultipleItemGroupStyle" TargetType="GroupItem">
<Setter Property="Padding" Value="0,0,1,0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupItem">
<Grid>
<Border Margin="{TemplateBinding Padding}" x:Name="grpOverlayBd" CornerRadius="4" Background="#FFAAAAAA" BorderThickness="0" BorderBrush="Transparent" >
<ItemsPresenter />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="SingleItemGroupStyle" TargetType="GroupItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupItem">
<ItemsPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
For starters i looked at CollectionViewGroup using reflector .
CollectionViewGroupis the DataContext applied by CollectionViewSource to a GroupItemGroupItem it creates.
It has a ItemsCount property and it implements INotifyPropertyChanged.
For example all items containing just one item are now red .