【发布时间】:2020-08-27 18:39:11
【问题描述】:
我是 WPF 新手,我正在尝试为我的应用程序中的按钮定义按钮样式。我添加了以下ResourceDictionary 并在App.xaml 的应用程序资源中添加了它:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ReciepeFinder">
<Style x:Key="NavButton" TargetType="Button">
<Setter Property="Width"
Value="120" />
<Setter Property="Height"
Value="120" />
<Setter Property="BorderThickness"
Value="0" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/ReciepeFinder;component/resources/images/home.png" />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/ReciepeFinder;component/resources/images/home_selected.png" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
基本上,在没有悬停时,我希望按钮显示resources/images/home.png,悬停时显示resources/images/home_selected.png。当我尝试在上面的代码中定义ControlTemplate 并运行应用程序时,按钮就不会出现。但是,如果我完全删除了ControlTemplate 标签,那么按钮就在那里,带有正确的Background,但显然没有悬停效果。
我做错了什么?
额外问题:如果我想使用绑定,以便我可以在其他按钮的属性中设置无悬停图像和悬停图像的路径并使其行为方式相同,我怎样才能做到这一点?
【问题讨论】:
标签: c# wpf xaml controltemplate