wpf - 如何在 WPF Window.Resources 中设置样式。

标签 wpf xaml resources window styles

我想在 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/

相关文章:

java - 如何将资源打印为字符串(struts java)

java - JSF 所需的库是什么/在哪里(特别是 javax.faces 的东西)

c# - 来自 Mosha Pasumansky 的 XAML 数据透视表

.net - 我可以将 WPF TreeView 绑定(bind)到单个根节点吗?

android - 为androidTest添加布局资源

WPF - 将 RadioButton 样式设置为 ToggleButton 有效,但在 VS 中显示错误

c# - 访问 UserControl 的内部控件

wpf - 如何正确绑定(bind)菜单项?

c# - WPF 使用 € 符号将字符串格式化为货币

c# - WPF 工具包扩展 - 将下拉按钮关闭事件绑定(bind)到 ViewModel 方法