如何在 WPF Window.Resources 中设置样式。
我想在 Window.Resources 中创建多种样式。下面是我尝试过的代码,但它不起作用:
<Window.Resources>
<Style x:Key="StyleOne" TargetType="{x:Type Control}">
<Setter Property="Control.Background" Value="Blue"></Setter>
<Setter Property="Control.Height" Value="20"></Setter>
</Style>
<Style x:Key="StyleTwo" BasedOn="{StaticResource StyleOne}">
<Setter Property="Control.Background" Value="Red"></Setter>
<Setter Property="Control.Height" Value="20"></Setter>
</Style>
</Window.Resources>
<Button Style="{StaticResource StyleOne}"></Button>
<Button Style="{StaticResource StyleTwo}"></Button>
它抛出一个错误:
属性“Content”被设置多次。
I want to create multiple styles in the Window.Resources. Below is the code I tried, but it's not working:
<Window.Resources>
<Style x:Key="StyleOne" TargetType="{x:Type Control}">
<Setter Property="Control.Background" Value="Blue"></Setter>
<Setter Property="Control.Height" Value="20"></Setter>
</Style>
<Style x:Key="StyleTwo" BasedOn="{StaticResource StyleOne}">
<Setter Property="Control.Background" Value="Red"></Setter>
<Setter Property="Control.Height" Value="20"></Setter>
</Style>
</Window.Resources>
<Button Style="{StaticResource StyleOne}"></Button>
<Button Style="{StaticResource StyleTwo}"></Button>
It throws an error saying:
The property "Content" is set more than once.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此错误与样式无关,窗口只能包含一个子窗口(它设置
Content
),使用某种可以包含多个子项的容器。例如StackPanel
或网格
。(另请参阅:面板概述)
This error has nothing to do with styles, the window can only contain one child (which sets the
Content
), use some container which can contain more than one child. e.g. aStackPanel
orGrid
.(See also: Panels Overview)
设置第二种样式的目标类型
将按钮放入堆栈面板或网格中
set the target type for the second style
put the buttons inside a stackpanel or Grid
我猜 BasedOn 继承了其他样式类型的属性,并且您已经
在两种样式中进行了设置,因此会出现错误
I guess BasedOn inherits the properties from other style type and you have
set in both the styles hence getting an error