如何将WPF效果颜色绑定到ControlTemplate的前景或背景

时间:2014-10-10 00:56:00

标签: c# wpf xaml controltemplate

当谈到绑定到ControlTemplate中的Effect的Color属性时,我看到了一些奇怪的行为。直接设置值时,我可以将颜色定义为字符串(例如" Red")或Hex值(例如#FFFF0000)。

但是,当使用绑定时,它仅在颜色被定义为String时才有效,这是ControlTemplate样式中的一个问题,因为我想使用TemplateParent属性的颜色,它们被绑定为Hex值

例如。看看下面的XAML(对不起,我知道它很长但我想展示所有情况):

<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

这是为什么?有办法解决吗?我希望能够在控件模板的效果中使用Button的Background和Foreground属性。

1 个答案:

答案 0 :(得分:5)

是的,Background是一个Brush对象,如果你的模板背景属性是纯色,那么将Color属性绑定到Background的color属性是可行的 {Binding RelativeSource = {RelativeSource TemplatedParent},Path = Background.Color} 或者你可以使用转换器。

更新了代码示例:

<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>