0

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?

akjoshi
  • 15,374
  • 13
  • 103
  • 121

1 Answers1

2

You can bind to the CollectionViewSource and access the View.Groups property, which is an observable collection of groups -- cvs.View.Groups.Count is what you are after.

Do keep in mind that even when there is just one server involved the user should be able to somehow identify which one.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • thanks, I was not able to find this; yes you are right but in my scenario server is a very technical detail and all clients won't need that, only few having multiple server need to know the server specific details. – akjoshi Sep 04 '12 at 12:35