David Green
David Green

Reputation: 1234

XAML - Binding Group Header In Group Header Style

Not trying to post a duplicate of this thread: WPF - Binding to current item from within group header style

I got the binding working. I'm stil unclear why it works. This part of the XAML sets up the group item and binds the Texbox to the text. What I don't understand is the Binding to the 'Name' property. What is 'Name' a property of? The group header? Until I found the linked thread I was trying to bind to a property on the items in the data grid.

 <ControlTemplate TargetType="{x:Type GroupItem}">
      <Expander IsExpanded="True"
                Background="#FF112255"
                BorderBrush="#FF002255"
                Foreground="#FFEEEEEE"
                BorderThickness="1,1,1,5">
         <Expander.Header>
           <StackPanel Orientation="Horizontal">
              <TextBlock FontWeight="Bold"
                         Text="SVC Node: "/>
                 <TextBlock FontWeight="Bold"
                            Text="{Binding Name}"/>
             </StackPanel>
           </Expander.Header>
         <Expander.Content>
        <ItemsPresenter />
       </Expander.Content>
      </Expander>
  </ControlTemplate>

Upvotes: 0

Views: 847

Answers (2)

mm8
mm8

Reputation: 169200

Name refers to the property of the System.Windows.Data.CollectionViewGroup object that gets created by WPF and being set as the DataContext of a GroupItem.

This property returns the value of the property that you group the source collection by. So if you for example group a source collection of Person objects by a property called Sex, the Name property of the CollectionViewGroup would return something like "Male" or "Female". This Name property is not a property of your custom model class.

The CollectionViewGroup class also has a an Items property that returns the collection of objects, for example all Person objects where the Sex property returns "Male", that belongs to the specific group.

Hope that makes sense.

Upvotes: 1

SharpGobi
SharpGobi

Reputation: 144

if your model contains the Name property and also GroupItem's DataContext contains same model object then Name property visible and able to access inside the control template. So that name property binding works in TextBlock.

Updated: based on my analysis , corresponding grouped property has assigned internally into group item's Name property, SO that it automatically derived to its childs, enter image description here

Upvotes: 0

Related Questions