locked
Can I bind to ListView header/footer within a style?

    Question

  • I'm trying to create a listview style with full customisation - including the header/footer area.

    I can't figure out how to bind properties to style the header/footer datatemplates.

    Here's the code I've got so far:

      <DataTemplate x:Key="GroganListViewHeader">
            <Grid Height="20">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid Grid.RowSpan="2">
                    <Border BorderBrush="{Binding Background, Converter={StaticResource ColourBrushToDarker}, FallbackValue=LightGray, TargetNullValue=LightGray}" BorderThickness="1" CornerRadius="0,0,6,6" Background="{Binding Background, Converter={StaticResource ColourBrushToDarker}, FallbackValue=LightGray, TargetNullValue=LightGray}" />
                </Grid>
                <Grid>
                    <Border BorderBrush="{Binding Background, FallbackValue=White, TargetNullValue=White}" BorderThickness="1" CornerRadius="0,0,6,6" Background="{Binding Background, FallbackValue=White, TargetNullValue=White}" />
                </Grid>
            </Grid>
        </DataTemplate>
    
        <Style TargetType="ListView" x:Key="GroganListView">
            <Setter Property="HeaderTemplate" Value="{StaticResource GroganListViewHeader}"></Setter>
    </Style>

    My code works in the context of a single listview, but I want to make it work as a style I can use in multiple apps.

    For example I want to bind the ListView's Header (text) property to the textblock defined in my header's DataTemplate. I also want to bind various colours set in the ListView control to colour the heading/footer areas as well.

    I'm not sure I'm going about this the right way. Any ideas?


    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.


    Monday, March 2, 2015 8:44 AM

All replies

  • What happens when you try this?


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, March 2, 2015 7:19 PM
    Moderator
  • Matt,

    When I use that code in a normal scenario (e.g. a user control) everything works perfectly. But I want to make this into a style.

    When I use that code in the context of a style I can't get any data binding in the data templates to bind. For example, I want to make the header template inherit the border brush colour from the ListView it's attached to. This works fine if I attach the data template to a single ListView, but fails to bind when I try to convert it to a style.


    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.

    Tuesday, March 3, 2015 12:24 AM