Germán Diago
Germán Diago

Reputation: 7673

How to bind ColorBrush from inside ControlTemplate to a custom property outside of ControlTemplate?

This binding is driving me crazy, tried all sort of (even supposedly identical) solutions from stackoverflow. I cannot figure out why a binding does not work...

Problem:

I have a style with a ControlTemplate that is applied to a GridViewColumnHeader. This is the style with the ControlTemplate property:

    <Style x:Key="GridHeaderWithInkColor" TargetType="{x:Type GridViewColumnHeader}">
      <Setter Property="Template">
         <Setter.Value>
            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
              <Border BorderThickness="0,0,0,1" BorderBrush="White">
                <StackPanel Margin="0,0,0,10" Orientation="Vertical">
                 <TextBlock Text="{TemplateBinding Content}" Padding="5" 
                                        Width="{TemplateBinding Width}" TextAlignment="Left"
                                                   FontSize="24" Foreground="White"/>
                <Rectangle Height="15" Width="70" Margin="10" RadiusX="5" RadiusY="5" StrokeThickness="2" Stroke="DarkGray" HorizontalAlignment="Center">
                  <Rectangle.Fill>
                    <SolidColorBrush Color="???????????????"/>                                                        
                  </Rectangle.Fill>
                </Rectangle>
             </StackPanel>
           </Border>
       </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

I use the template as following in a GridViewHeaderColumn:

<GridViewColumn HeaderContainerStyle="{StaticResource GridHeaderWithInkColor}" local:ExtendedProps.InkColor="{Binding MachineInfo.BarColorsList[0]}"/>

local:ExtendedProps.InkColor is a property I defined myself to be able to use it in xaml.

**Question is:

  1. How can I bind SolidColorBrush inside ControlTemplate to property local:ExtendedProps.InkColor in GridViewColumn at the time of use.**

I tried this (and other variations, templatebindings, etc.) with no luck:

<SolidColorBrush Color="{Binding DataContext.IsItemSelected,
                         RelativeSource={RelativeSource AncestorType=GridViewColumn}}"/>                                                        

The two things that might be doing my life difficult are (I think):

Upvotes: 0

Views: 422

Answers (1)

mm8
mm8

Reputation: 169380

How can I bind SolidColorBrush inside ControlTemplate to property local:ExtendedProps.InkColor in GridViewColumn at the time of use.

The GridViewColumnHeader has a Column property that you can use to bind a property of the GridViewColumn:

Color="{Binding Path=Column.(local:ExtendedProps.InkColor), 
    RelativeSource={RelativeSource AncestorType=GridViewColumnHeader}}"

The column isn't a visual ancestor of the header so using {RelativeSource AncestorType=GridViewColumn} won't work.

Upvotes: 1

Related Questions