Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind WPF Effect Color to Foreground or Background of ControlTemplate

I am seeing some odd behaviour when it comes to Binding to the Color property of an Effect within a ControlTemplate. When setting the value directly, I can define the color as a string (eg. "Red") or as a Hex value (eg. #FFFF0000).

However, when using binding, it ONLY works if the color is defined as a String, which is an issue in a ControlTemplate style, as I would like to use the colors of the TemplateParent properties, which get bound to as Hex values.

For example. Take a look at the following XAML (sorry I know it is long but I wanted to show all cases):

<Window x:Class="EffectTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>

        <!-- 
        STYLE 1 
        This works, as the Color is hard coded, but note that the hard 
        coded value is identicle to the value in Style 2 (which doesn't work).
        -->
        <Style x:Key="Style1" TargetType="{x:Type Button}">
            <Style.Setters>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Width="{TemplateBinding Width}"
                                    Height="{TemplateBinding Height}"
                                    Background="{TemplateBinding Foreground}">
                                <Border.Effect>
                                    <DropShadowEffect Color="#FFFF0000"/>
                                </Border.Effect>
                                <TextBlock Foreground="White" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>

        <!--
        STYLE 2
        This fails (the dropshadow appears black) even through the value being bound to is the same as Style 1.
        -->
        <Style x:Key="Style2" TargetType="{x:Type Button}">
            <Style.Setters>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Width="{TemplateBinding Width}"
                                    Height="{TemplateBinding Height}"
                                    Background="{TemplateBinding Foreground}">
                                <Border.Effect>
                                    <!--NOTE: TemplateBinding does not work at all... <DropShadowEffect Color="{TemplateBinding Background}"/>-->
                                    <DropShadowEffect Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}"/>
                                </Border.Effect>
                                <TextBlock Foreground="White" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>

        <!--
        STYLE 3
        This works, but note that I am binding to "Name" for the Color, which just happens to be a valid color "Red".
        -->
        <Style x:Key="Style3" TargetType="{x:Type Button}">
            <Style.Setters>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Width="{TemplateBinding Width}"
                                    Height="{TemplateBinding Height}"
                                    Background="{TemplateBinding Foreground}">
                                <Border.Effect>
                                    <DropShadowEffect Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Name}"/>
                                </Border.Effect>
                                <TextBlock Foreground="White" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Name}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
    </Window.Resources>

    <StackPanel Width="150">
        <Button Style="{StaticResource Style1}" Foreground="LightBlue" Background="Red"></Button>
        <Separator Visibility="Hidden" Height="5"/>
        <Button Style="{StaticResource Style2}" Foreground="LightBlue" Background="Red"></Button>
        <Separator Visibility="Hidden" Height="5"/>
        <Button Style="{StaticResource Style3}" Foreground="LightBlue" Name="Red"></Button>
    </StackPanel>
</Window>

Results in:

Results

Why is this? Is there a way around it? I would like to be able to use the Background and Foreground properties of Button inside the control template's effect.

like image 803
Goose Avatar asked Oct 10 '14 00:10

Goose


1 Answers

Yes,Background is a Brush object and if your template background property is a pure color then it is feasible for you to bind Color property to Background's color property like {Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background.Color} Or you can either use a converter.

Updated code sample:

<Style x:Key="Style2" TargetType="{x:Type Button}">
    <Style.Setters>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}"
                            Background="{TemplateBinding Foreground}">
                        <Border.Effect>
                            <!-- Now uses Background.Color -->
                            <DropShadowEffect Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background.Color}"/>
                        </Border.Effect>
                        <TextBlock Foreground="White" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background.Color}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>
like image 61
Andrew Carl Avatar answered Oct 04 '22 02:10

Andrew Carl