将背景绑定到模板化控件

本文关键字:模板化控件 绑定 背景 | 更新日期: 2023-09-27 18:07:48

我有以下Checkbox.xaml文件。我想在鼠标上应用两种不同的Dropshadow效果在CheckBox上。我取了两个MultiTriggers。我想删除这些MultiTriggers,并使用DataBindingBorder元素的Background将它们组合成单个触发器。因为Border元素总是有右背景,因为我已经应用了第一个触发器。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="..'Colors'Colors.xaml"></ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
    <ControlTemplate x:Key="CheckBoxTemplate" TargetType="CheckBox" >
       <BulletDecorator >
        <BulletDecorator.Bullet>
            <Border x:Name="Border"  
              Width="13" 
              Height="13" 
              CornerRadius="0" 
              Background="{StaticResource TextNormalBrush}"
              BorderThickness="3"
              BorderBrush="Black">
                </Border>
        </BulletDecorator.Bullet>
        <ContentPresenter Margin="4,0,0,0"
            VerticalAlignment="Center"
            HorizontalAlignment="Left"
            RecognizesAccessKey="True" />
        </BulletDecorator>
        <ControlTemplate.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter TargetName="Border" Property="Background" Value="{StaticResource AlternateLinearBrush}"/>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver" Value="True"></Condition>
                    <Condition Property="IsChecked" Value="False"></Condition>
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter TargetName="Border" Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect ShadowDepth="0" Direction="0" BlurRadius="15" Opacity="1" Color="{StaticResource TextNormalColor }">
                            </DropShadowEffect>
                        </Setter.Value>
                    </Setter>
                </MultiTrigger.Setters>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver" Value="True"></Condition>
                    <Condition Property="IsChecked" Value="True"></Condition>
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter TargetName="Border" Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect ShadowDepth="0" Direction="0" BlurRadius="15" Opacity="1" Color="{StaticResource AlternateColor }">
                            </DropShadowEffect>
                        </Setter.Value>
                    </Setter>
                </MultiTrigger.Setters>
            </MultiTrigger>                
        </ControlTemplate.Triggers>            
    </ControlTemplate>
    <Style x:Key="CheckBoxStyle" TargetType="{x:Type CheckBox}">
        <Setter Property="Template" Value="{StaticResource CheckBoxTemplate}" />
        <Setter Property="Foreground" Value="{StaticResource TextNormalBrush}" />
        <Setter Property="TextOptions.TextHintingMode" Value="Animated"/>
    </Style>
</ResourceDictionary>

将背景绑定到模板化控件

你的意思是:

        <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="Border" Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect ShadowDepth="0" Direction="0" BlurRadius="15" Opacity="1" Color="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=Background.Color}">
                        </DropShadowEffect>
                    </Setter.Value>
                </Setter>
        </Trigger>

在这个例子中,阴影效果总是有它的边框的背景色