How to tie a brush color to an element?

I do this, but it does not work:

<Window.Resources> <Style TargetType="{x:Type Button}"> <Style.Resources> <SolidColorBrush x:Key="DefaultColor" Color="{TemplateBinding Background}" /> </Style.Resources> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Ellipse Fill="{StaticResource DefaultColor}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Button Content="Button1" Background="Red"/> </Grid> 

    2 answers 2

    It works like this for me (with a revised converter: it should return SolidColorBrush , not Color ):

     <Style TargetType="{x:Type Button}"> <Style.Resources> <local:ColorConverter x:Key="ColorLightConverter"/> </Style.Resources> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Grid> <Ellipse Name="E" Fill="{TemplateBinding Background}"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="E" Property="Fill" Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ColorLightConverter}, ConverterParameter=LIGHT}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 

    For some reason, Setter does not want to accept a TemplateBinding , but instead you can do a Binding to a TemplatedParent , which is practically the same.

    • Thank! Exactly what is needed. - trydex
    • @maxwell: Good. You are welcome! - VladD
     <Style TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Ellipse Fill="{TemplateBinding Background}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> 
    • This is understandable, but how exactly does a brush go? - trydex
    • @maxwell, bind on what? I understand you want to set the background of the button with an overridden pattern. - Gardes
    • This is just an example. I want the color of a specific item to be stored in the brush. In this case, the color of the button. - trydex
    • @maxwell, then ask Fill? - Gardes
    • one
      @maxwell: Ok, figured out. Now I will write the answer. - VladD