因此,愿望很简单,将按钮的背景更改为 LightGreen,鼠标光标悬停在其上时为绿色,按下时为 DarkGreen。以下 XAML 是我的第一次尝试:
<Window x:Class="ButtonMouseOver.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">
<Grid>
<Button Background="LightGreen">
Hello
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property = "Background" Value="Green"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property = "Foreground" Value="DarkGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>
但是,唉,这行不通。我们距离目标只有 1/3。是的,按钮变为浅绿色,但只要将鼠标悬停在它上面或按下它,您就会获得相应按钮状态的标准 Aero chrome。不想放弃,我尝试了以下放荡:
...
<Button xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
Background="LightGreen">
Hello
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Microsoft_Windows_Themes:ButtonChrome SnapsToDevicePixels="true"
x:Name="Chrome" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding IsDefaulted}"
RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"
>
<ContentPresenter Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Microsoft_Windows_Themes:ButtonChrome>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property = "Background" Value="Green"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property = "Foreground" Value="DarkGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
...
现在我们已经从Aero.NormalColor.xaml中取出了整个该死的控制模板。唉,这并没有改变。然后我开始从元素中删除两个属性 (RenderPressed
和RenderMouseOver
) 。Microsoft_Windows_Themes:ButtonChrome
不过,没有任何变化。然后我删除 Button 的Background
属性设置,只留下 PresentationFramework.Aero 程序集的命名空间声明:
...
<Button xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
Hello
...
最后,我们取得了进展。我们已经从 1/3 的需求减少到 2/3,IsMouseOver 和 IsPressed 工作,但在按钮的正常状态(没有被鼠标悬停或按下)期间没有浅绿色背景,因为我已经删除了按钮的 Background 属性,以获取其他两种状态以应用并可见。现在,在这个疯狂的 XAML 未能为我们提供正常按钮状态的 LightGreen 背景之后,我修改了样式以将背景颜色也放入其中:
<Button xmlns...
<ControlTemplate...
...
</ControlTemplate>
<Button.Style>
<Style TargetType="Button">
<!-- ##### Normal state button background added ##### -->
<Setter Property="Background" Value="LightGreen" />
<!-- ################################################ -->
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property = "Background" Value="Green"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property = "Background" Value="DarkGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
最后,它按预期工作。
我是不是疯了,或者这些(以及更多)你必须通过 Aero 主题来改变按钮的背景颜色在它的一些不同状态(正常、悬停、按下)?也许,如果我不必在其中包含整个 ButtonTemplate 只是为了从中删除两个属性 (RenderMouseOver
和RenderPressed
),那么生活不会那么糟糕。
感谢您的帮助-我无能为力。
更新:但是等等还有更多。而不是从控件模板中删除 RenderMouseOver 和 RenderPressed 属性,只需将它们更改为:
<Microsoft_Windows_Themes:ButtonChrome SnapsToDevicePixels="true" ...
RenderMouseOver="{TemplateBinding IsMouseOver}"
RenderPressed="{TemplateBinding IsPressed}" ...
到:
<Microsoft_Windows_Themes:ButtonChrome SnapsToDevicePixels="true" ...
RenderMouseOver="{Binding IsMouseOver}"
RenderPressed="{Binding IsPressed}" ...
...,还解决了问题(忽略按钮的样式触发器)。我认为问题的根源在于模板绑定是在编译时应用的(出于性能原因),而常规绑定是在运行时应用的。因此,如果属性使用模板绑定(即使用原始模板中的 IsMouseOver 和 IsPressed),则不会使用来自单个按钮的样式触发器的绑定。
说了以上所有内容,下面是在 Aero 中更改按钮背景同时保留其原始控件模板的规范示例:
<Window x:Class="ButtonMouseOver.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">
<Grid>
<Button>
Hello
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Microsoft_Windows_Themes:ButtonChrome SnapsToDevicePixels="true"
x:Name="Chrome" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding IsDefaulted}"
RenderMouseOver="{Binding IsMouseOver}" RenderPressed="{Binding IsPressed}"
>
<ContentPresenter Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Microsoft_Windows_Themes:ButtonChrome>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="LightGreen"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property = "Background" Value="Green"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property = "Foreground" Value="DarkGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>
这当然是假设只有一个按钮具有这种样式,否则模板和样式可以移出到某处的资源部分。此外,按钮的默认状态没有触发器,但很容易将其添加到上面的示例中(不要忘记将控件模板中的 RenderDefaulted 属性更改为使用 Binding 而不是 TemplateBinding)。
如果有人对如何仅针对控件模板中需要更改的两个属性使用 Binding覆盖TemplateBinding 有任何建议,而无需从 aero xaml 批发中重复整个 chrome 定义,我会全力以赴。