【发布时间】:2013-04-28 19:53:05
【问题描述】:
我有一个Button,其自定义外观由ControlTemplate 定义。
它包含一个Canvas,其中包含一个Path。我想根据鼠标状态在Path.Opacity 中添加更改:
- 默认 - 0.5
- 鼠标悬停,未按下 - 1.0
- 鼠标悬停,按下 - 0.5
只需将Path.Opacity 的本地值设置为0.5 并为Trigger 添加一个Trigger,即可涵盖第一种情况:
<Button x:Class="ImagingShop.Panosphere.Controls.PathButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="100"
Name="pathButton">
<Button.Template>
<ControlTemplate>
<Canvas Background="Transparent" Width="{TemplateBinding Width}" Height="{TemplateBinding Width}">
<Canvas.Style>
<Style TargetType="Canvas">
<Setter Property="Path.Opacity" Value="0.5"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Path.Opacity" Value="1.0"/>
</Trigger>
</Style.Triggers>
</Style>
</Canvas.Style>
<Path Data="{Binding ElementName=pathButton, Path=PathData}" Stretch="Uniform" Fill="#FFFFFFFF" Width="{TemplateBinding Width}" Height="{TemplateBinding Width}"/>
</Canvas>
</ControlTemplate>
</Button.Template>
<Grid>
</Grid>
</Button>
但是,第三种情况不起作用。我添加了以下触发器:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
<Condition Property="Button.IsPressed" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Path.Opacity" Value="0.75"/>
</MultiTrigger>
所以这应该将Path.Opacity 设置为0.75 是鼠标悬停在按钮上并且按钮被按下。
由于不透明度更改为0.5 而不是0.75,我对此感到困惑!触发器似乎适用,但没有按预期工作...
【问题讨论】:
-
那么 - 不能仅使用 XAML 重新设置按钮的样式吗?看来 Button.IsPressed 应该可以工作 - 有一个 XAML example 正在这样做。
标签: wpf button triggers controltemplate