【发布时间】:2011-05-24 09:46:13
【问题描述】:
因此,愿望很简单,将按钮的背景更改为浅绿色,当鼠标光标悬停在按钮上时为绿色,按下时为深绿色。以下 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 中提取了整个该死的控制模板。唉,这并没有改变。然后我开始从Microsoft_Windows_Themes:ButtonChrome 元素中删除两个属性(RenderPressed 和RenderMouseOver)。不过,没有任何变化。然后我删除 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)。
如果有人对如何覆盖 TemplateBinding with Binding 仅针对控件模板中需要更改的两个属性有任何建议,而无需从 aero xaml 批发中重复整个 chrome 定义,我我全神贯注。
【问题讨论】:
标签: c# wpf c#-4.0 wpf-controls wpf-4.0