Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to create an ItemsControl which is charged with displaying various items sorted by metadata. Each item can have multiple metadata. For example, an item of type "Conversation" could have a "Chapter", "Act" and "Volume" metadata.

Upon searching how grouping worked in WPF, I found out about GroupStyles and the PropertyGroupDescription class.

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

Is there anyway that in my ItemsControl's GroupStyle DataTemplate I can bind to the object returned (i.e.: the "Metadata" object) and then display its properties as I wish ? Or am I forced to bind 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>
like image 638
Hussein Khalil Avatar asked Apr 19 '12 17:04

Hussein Khalil


People also ask

How many types of binding are there in WPF?

WPF binding offers four types of Binding.

How does binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

Where to place style in WPF?

You can use a style on any element that derives from FrameworkElement or FrameworkContentElement such as a Window or a Button. The most common way to declare a style is as a resource in the Resources section in a XAML file. Because styles are resources, they obey the same scoping rules that apply to all resources.

How to inherit style in WPF?

When you inherit a style from an existing style, the settings from a parent style are available in the inherited style. To inherit a style from another style, we set the BasedOn property to StaticResource Markup Extension as the style it is being inherited from.


1 Answers

By default DataContext in GroupStyle you can bind to this CollectionViewGroup Properties. But of course you can set any other BindingSource if you need.

If you want the first object in your group use:

<TextBlock Text="{Binding Path=Items[0].YourStringProperty}" />
like image 140
LPL Avatar answered Sep 18 '22 16:09

LPL