zpete
zpete

Reputation: 1765

Binding within a ListView GroupStyle Header

I use a ListView.GroupStyle in conjunction with a HeaderTemplate to style ListView Headers.

<ListView.GroupStyle>
    <GroupStyle HeaderTemplate="{StaticResource headerTemplate}" />
</ListView.GroupStyle>

...

<DataTemplate x:Key="headerTemplate">
        <Border Opacity=".9" Padding="0,5,0,5" Margin="0" BorderThickness="0" BorderBrush="LightGray" HorizontalAlignment="Stretch" Background="#ffffff">
            <Grid Margin="3" HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="3*"/>
                </Grid.ColumnDefinitions>
                <TextBlock 
                    Grid.Column="0" 
                    Text="{Binding Path=Name}"
                    HorizontalAlignment="Right"/>

                <StackPanel Grid.Column="1" Orientation="Horizontal">
                    <Button Margin="5" Padding="5"     
                    HorizontalAlignment="left" 
                    Content="{Binding ???}" 
                    Command="{Binding ???}" />
                </StackPanel>

            </Grid>
        </Border>
    </DataTemplate>

Now I would like to put controls in that headerTemplate - e.g. a Button to execute actions to all entries within that group. How do I do this? How is Binding done within a GroupStyle HeaderTemplate?

Upvotes: 0

Views: 1146

Answers (1)

Sheridan
Sheridan

Reputation: 69959

You can use a RelativeSource Binding to access the DataContext of the GridView from the HeaderTemplate:

<DataTemplate x:Key="headerTemplate">
    <Border Opacity=".9" Padding="0,5,0,5" ...>
        <Grid Margin="3" HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="3*"/>
            </Grid.ColumnDefinitions>
            <TextBlock 
                Grid.Column="0" 
                Text="{Binding Name}"
                HorizontalAlignment="Right"/>
            <StackPanel Grid.Column="1" Orientation="Horizontal">
                <Button Margin="5" Padding="5"     
                HorizontalAlignment="left" 
                Content="{Binding DataContext.SomeProperty, RelativeSource={
                    RelativeSource AncestorType={x:Type GridView}}}" 
                Command="{Binding DataContext.AnotherProperty, RelativeSource={
                    RelativeSource AncestorType={x:Type GridView}}}" />
            </StackPanel>
        </Grid>
    </Border>
</DataTemplate>

Upvotes: 1

Related Questions