WPF - Group Styles: Can we only bind to the "Name" property?

I am trying to create an ItemsControl that is tasked with displaying various items sorted by metadata. Each item can have multiple metadata. For example, an item of type "Conversation" may have metadata "Chapter", "Action" and "Volume".

After finding group work in WPF, I found out about GroupStyles and the PropertyGroupDescription class.

I created my own class that inherits from PropertyGroupDescription and returns an object of type Metadata.

Is there anyway that in my DataControl GroupStyle DataTemplate I can bind to the returned object (ie, the "Metadata" object) and then display its properties as I wish? Or am I forced to become attached to "Name"?

In other words:

<ItemsControl.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> Am I forced to bind to "Name" here ? </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </ItemsControl.GroupStyle> 
+6
source share
2 answers

By default, a DataContext in GroupStyle you can bind CollectionViewGroup properties to this. But, of course, you can install any other BindingSource .

If you want the first object in your group to use:

 <TextBlock Text="{Binding Path=Items[0].YourStringProperty}" /> 
+14
source

By default, when binding to a custom object, it will try to display using the ToString () method, rather than Name (well, ToString () still prints the default name). You can try to override the ToString () method to print something else, you will see what I mean.

But, of course, you can define your own DataTemplate and binding to another property.

 <DataTemplate> <TextBlock Text="{Binding XXXXX}"/> </DataTemplate> 
0
source

Source: https://habr.com/ru/post/913642/


All Articles