如何:将 generic.xaml 中定义的样式应用于 UserControl? (WPF)
我在 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。尝试运行时出现运行时错误。
提前致谢。
I've created a style in generic.xaml that i want to use in my project on several UserControls. In the same way i have defined a style for a custom control and this one works so it seems generic.xaml is loaded, this is the defined style:
<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>
But now i want to use this style but i cannot get it to work. i've tryed adding it as a style parameter to a custom instance of UserControl in the following way:
<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}">
however this gives me a note that the resource ServiceStyle could not be found. When trying to run there is a runtime error on this.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来 Generic.xaml 不是存储非自定义控件样式的正确位置。我在某个地方发现了将 syle 放入 App.xaml 而不是 generic.xaml 的提示,这直接起作用了。因此,Generic.xaml 只能用于存储 customControls 的样式。
也许有人可以为这种行为添加更有根据的理由?
It seems that Generic.xaml is not the right place to store styles for non custom controls. Somewhere i found the hint to put the syle in App.xaml instead of generic.xaml and this worked directly. So it seams Generic.xaml can only be used to store styles for customControls.
Maybe someone can add a more educated reason for this behaviour?
当 Generic.xaml 使用 ComponentResourceKey 时。
When Generic.xaml use ComponentResourceKey.