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

发布于 2025-01-03 23:49:35 字数 816 浏览 4 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

古镇旧梦 2025-01-10 23:49:35

此错误与样式无关,窗口只能包含一个子窗口(它设置 Content),使用某种可以包含多个子项的容器。例如 StackPanel网格

<StackPanel>
     <Button .../>
     <Button .../>
</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. a StackPanel or Grid.

<StackPanel>
     <Button .../>
     <Button .../>
</StackPanel>

(See also: Panels Overview)

枕头说它不想醒 2025-01-10 23:49:35

设置第二种样式的目标类型

 <Style x:Key="StyleTwo"
           BasedOn="{StaticResource StyleOne}"
           TargetType="{x:Type Control}">
        <Setter Property="Control.Background"
                Value="Red"></Setter>
        <Setter Property="Control.Height"
                Value="20"></Setter>
    </Style>

将按钮放入堆栈面板或网格中

set the target type for the second style

 <Style x:Key="StyleTwo"
           BasedOn="{StaticResource StyleOne}"
           TargetType="{x:Type Control}">
        <Setter Property="Control.Background"
                Value="Red"></Setter>
        <Setter Property="Control.Height"
                Value="20"></Setter>
    </Style>

put the buttons inside a stackpanel or Grid

辞慾 2025-01-10 23:49:35

我猜 BasedOn 继承了其他样式类型的属性,并且您已经

    Property="Control.Background"

在两种样式中进行了设置,因此会出现错误

    "The property "Content" is set more than once."

I guess BasedOn inherits the properties from other style type and you have

    Property="Control.Background"

set in both the styles hence getting an error

    "The property "Content" is set more than once."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文