Wpf 如果目标属性已具有显式值,则属性触发器中的Setter将失败

Wpf 如果目标属性已具有显式值,则属性触发器中的Setter将失败,wpf,xaml,controltemplate,Wpf,Xaml,Controltemplate,我目前正在尝试为WPF中的按钮类创建一个控制模板,用一个类似于Google Chrome标签上的小(X)关闭图标的东西替换通常的视觉树。我决定在XAML中使用路径对象来实现这个效果。使用属性触发器,控件通过设置图标的红色背景来响应IsMouseOver属性中的更改 以下是测试应用程序中的XAML: <Window x:Class="Widgets.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/present

我目前正在尝试为WPF中的
按钮
类创建一个
控制模板
,用一个类似于Google Chrome标签上的小(X)关闭图标的东西替换通常的视觉树。我决定在XAML中使用
路径
对象来实现这个效果。使用属性触发器,控件通过设置图标的红色背景来响应IsMouseOver属性中的更改

以下是测试应用程序中的XAML:

<Window x:Class="Widgets.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

  <Window.Resources>
    <Style x:Key="borderStyle" TargetType="Border">
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Background">
            <Setter.Value>
              <SolidColorBrush Color="#CC0000"/>
            </Setter.Value>
          </Setter>
        </Trigger>
      </Style.Triggers>
    </Style>

    <ControlTemplate x:Key="closeButtonTemplate" TargetType="Button">
      <Border Width="12" Height="12" CornerRadius="6"
              BorderBrush="#AAAAAA" Background="Transparent"
              Style="{StaticResource borderStyle}"
              ToolTip="Close">
        <Viewbox Margin="2.75">
          <Path Data="M 0,0 L 10,10 M 0,10 L 10,0" Stroke="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType=Border, AncestorLevel=1}}" StrokeThickness="1.8"/>
        </Viewbox>
      </Border>
    </ControlTemplate>
  </Window.Resources>

  <Grid Background="White">
    <Button Template="{StaticResource closeButtonTemplate}"/>
  </Grid>

</Window>

请注意,圆形背景始终存在-当鼠标不在其上时,它是透明的

问题是触发器不起作用。按钮的外观没有任何变化。但是,如果我从
ControlTemplate
中的
Border
对象中删除
Background=“Transparent”
值,触发器确实会工作(尽管只有在“X”上方)

我真的无法解释。放置在
borderStyle
资源中的任何其他属性的设置器工作正常,但是
Background
设置器在
ControlTemplate
中指定默认背景后就会失败

你知道为什么会这样吗?我该怎么解决?我知道我可以很容易地用一个基于.PNG的图像来替换这段代码,但是我想知道为什么当前的实现不起作用

谢谢!:)

尝试将显式“背景”赋值从边界声明内部移动到样式本身:

<Style x:Key="borderStyle" TargetType="Border">
    <Setter Property="Background" Value="Transparent" />

    <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
            <Setter Property="Background"> 
            <Setter.Value> 
                <SolidColorBrush Color="#CC0000"/> 
            </Setter.Value> 
        </Setter> 
    </Trigger> 
</Style.Triggers> 

...

<Border Width="12" Height="12" CornerRadius="6" 
        BorderBrush="#AAAAAA" 
        Style="{StaticResource borderStyle}" 
        ToolTip="Close"> 

...
样式不能替代已显式设置的属性。您需要在样式中设置值。

尝试将明确的“背景”赋值从边框声明内部移动到样式本身:

<Style x:Key="borderStyle" TargetType="Border">
    <Setter Property="Background" Value="Transparent" />

    <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
            <Setter Property="Background"> 
            <Setter.Value> 
                <SolidColorBrush Color="#CC0000"/> 
            </Setter.Value> 
        </Setter> 
    </Trigger> 
</Style.Triggers> 

...

<Border Width="12" Height="12" CornerRadius="6" 
        BorderBrush="#AAAAAA" 
        Style="{StaticResource borderStyle}" 
        ToolTip="Close"> 

...

样式不能替代已显式设置的属性。您需要在样式中设置值。

我认为您的问题在于边框在透明时不会“捕获”鼠标事件


验证-尝试将背景更改为#01FFFFFF而不是透明。

我认为您的问题在于,边框在透明时不会“捕获”鼠标事件


验证-尝试将背景更改为#01FFFFFF而不是透明。

不,不是这样。我特别使用透明,因为它可以捕捉鼠标事件(不像空笔刷)。我特别使用透明,因为它可以捕捉鼠标事件(不像空笔刷)。哇,这很有效-谢谢!不知道我以前怎么没见过这个。您是否有描述这种风格限制的参考资料(如MSDN文章)?我不知道,但这里经常会出现。:)找到MSDN上的参考资料:非常感谢你,马特!你刚刚把我的头从桌子上救了出来。哇,真管用——谢谢!不知道我以前怎么没见过这个。您是否有描述这种风格限制的参考资料(如MSDN文章)?我不知道,但这里经常会出现。:)找到MSDN上的参考资料:非常感谢你,马特!你刚刚把我的头从桌子上救了出来。