Changing (Enable/Disable) GroupStyle in ListView for different category items
Question
How can I switch between GroupStyles
for a ListView
based on some conditions at run time? For instance I need to use Default for items that that have GroupStyle
Header name null, and if it is not null then use the custom GroupStyle
theme? I tried GroupStyleSelector
, and it doesn't work, because it works for multi level grouping, and in my case I have only one level grouping.
If yes, then how?
Custom GroupStyle
:
<Style x:Key="grouping"
TargetType="{x:Type GroupStyle}">
<Setter Property="ContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin"
Value="0,0,0,5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False"
BorderBrush="#FFA4B97F"
BorderThickness="0,0,0,1">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold"
Text="{Binding Name}"
Margin="0"
Width="250" />
<TextBlock FontWeight="Bold"
Text="{Binding Path=Items[0].StartTime, StringFormat=T}" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
Thanks a lot.
Sincerely, Vlad.
Solution
Okay,
I found solution for it. Basically I needed to build DataTrigger and check for category in it, and if it matches, use different GroupStyle. Here is the example:
<ControlTemplate TargetType="{x:Type GroupItem}"
x:Key="defaultGroup">
<ItemsPresenter />
</ControlTemplate>
<ListView.GroupStyle>
<GroupStyle >
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin"
Value="0,0,0,5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False"
BorderBrush="Black"
BorderThickness="3"
Padding="5,1,1,5">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold"
Margin="0"
Width="250">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} ({1} jobs)">
<Binding Path="Name" />
<Binding Path="ItemCount" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock FontWeight="Bold"
Text="{Binding Path=Items[0].Category, StringFormat=T}" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Items[0].Category}"
Value="ABC">
<Setter Property="Template"
Value="{StaticResource defaultGroup}" />
</DataTrigger>
</Style.Triggers>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow