我在 generic.xaml 中创建了一个样式,我想在我的项目中的几个 UserControls 上使用它。以同样的方式,我为自定义控件定义了一种样式,并且这个样式有效,因此看起来 generic.xaml 已加载,这是定义的样式:
<Style TargetType="{x:Type UserControl}" x:Key="ServiceStyle" x:Name="ServiceStyle">
<Setter Property="Opacity" Value="0.5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type UserControl}">
<Border Name="border" CornerRadius="20"
Margin="10"
BorderThickness="5"
BorderBrush="Black">
<ContentPresenter Content="{TemplateBinding Content}"
Margin="{TemplateBinding Padding}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但现在我想使用这种风格,但我无法让它发挥作用。我尝试通过以下方式将其作为样式参数添加到 UserControl 的自定义实例中:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Netcarity"
xmlns:CustomControls="clr-namespace:Netcarity.CustomControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Portier_deur" x:Class="Netcarity.UserControlPortier"
Height="600" Width="800" MouseDown="UserControl_MouseDown" Loaded="UserControl_Loaded" mc:Ignorable="d"
Style="{StaticResource ServiceStyle}">
但是,这给了我一个提示,即找不到资源 ServiceStyle。尝试运行时,会出现运行时错误。
提前致谢。