【发布时间】:2018-07-14 00:11:00
【问题描述】:
我正在为我制作一个 DLL,以减轻我的工作,因为我在每个项目中都会使用一些类,所以我为什么要复制它们,当我可以使用一个 DLL 来完成工作时,
我还想给它添加一些控件,按钮,所以它是这样的:
我创建了一个按钮,效果很好,但是我想给它添加一个自定义样式,当你鼠标悬停时禁用背景突出显示,现在我以前使用过这个样式并且效果很好,但是在以前有时,我会将样式添加到 app.xaml 资源中,然后将样式设置为按钮,例如:
Style="{StaticResource DisableBackgroundHighlight}"
但是由于DLL没有app.xaml,我该怎么办,怎么给DLL里面的控件添加样式呢? 我在谷歌上找到的只是将 DLL 中的资源引用到 WPF 应用程序的 app.xaml 中,但这不是我想要的, 我试过这个:
<Button x:Class="SRX.Windows.Controls.SRXButton"
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"
xmlns:local="clr-namespace:SRX.Windows.Controls"
mc:Ignorable="d"
d:DesignHeight="35" d:DesignWidth="100" Content="OK" Background="White" BorderBrush="Blue" Foreground="Blue" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Style="{StaticResource DisableBackgroundHighlight}">
<Button.Resources>
<Style x:Key="DisableBackgroundHighlight" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border" BorderThickness="0" BorderBrush="Black" Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0.8" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Resources>
但它不起作用,它显示“资源“DisableBackgroundHighlight”无法解析。”虽然它编译但在启动时崩溃。 如果我在问题说明中遗漏了什么,请让我解决,提前感谢。
【问题讨论】: