我想在 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>
它抛出一个错误说:
The property "Content" is set more than once.
最佳答案
此错误与样式无关,窗口只能包含一个子项(设置 Content
),请使用一些可以包含多个子项的容器。例如 StackPanel
或 Grid
.
<StackPanel>
<Button .../>
<Button .../>
</StackPanel>
(另见:Panels Overview)
关于wpf - 如何在 WPF Window.Resources 中设置样式。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9203167/