I am having a list of messages and I am grouping them based on server(serverId) each message belongs to, like this -
<CollectionViewSource x:Key="MessageTypesByServer"
Source="{Binding MessageTypes, Mode=OneWay}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="ServerId" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
and using a Group Style
to display each group in a GroupBox
like this -
<GroupBox Margin="0,0,0,3"
Header="{Binding Name}">
<ItemsPresenter />
</GroupBox>
Now, if the messages are from single server then I don't want to display the serverId in header of GroupBox
, for doing this I need to find out the number of groups which are there in CollectionViewSource
;
How can I do this in XAML? (I know I can find this is in my VM and hide/show header based on a property in VM etc. but I would like to do this in XAML).
Update:
I tried to use the CollectionViewSource.View like this -
<Style TargetType="{x:Type GroupBox}">
<Setter Property="Header" Value="{Binding Name}" />
<Style.Triggers>
<DataTrigger Binding="{Binding View.Groups.Count, Source={StaticResource MessageTypesByServer}}"
Value="1">
<Setter Property="Header" Value="No Server Name" />
</DataTrigger>
</Style.Triggers>
</Style>
but this doesn't work, View is always null(when seen in Snoop).
Update 2
I was able to get it working with a converter like this -
<DataTrigger Binding="{Binding Path=., Source={StaticResource MessageTypesByServer},
Converter={ValueConverters:CollectionViewSourceToGroupCountConverter}}"
Value="1">
<Setter Property="Header" Value="{x:Static System:String.Empty}" />
</DataTrigger>
and the converter logic -
ICollectionView view = value as ICollectionView;
if (view != null)
{
return view.Groups.Count;
}
return 0;
although it is fine, I will be happy if I can do this without converter! Anyone having idea why the code in 1'st update doesn't work?