为什么将 WPF 样式应用于父控件?

发布于 2024-10-17 04:33:49 字数 759 浏览 1 评论 0原文

我定义了一个自定义 WPF 样式。我希望网格中的任何按钮都是红色的。但如果我定义这种样式,整个网格都是红色的!!为什么?我明确定义了 Button.Background。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style x:Key="MyStyle">
            <Setter Property="Button.Background" Value="Red" /> <!-- Only inner buttons -->
        </Style>            
    </Window.Resources>

    <Grid Style="{StaticResource MyStyle}">
        <Button Content="Go" Margin="29,36,385,239" />
    </Grid>
</Window>

I defined a custom WPF Style. I want any button in the Grid is Red. But if I define this style, the whole grid is red!! Why? I explicitly defined Button.Background.

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style x:Key="MyStyle">
            <Setter Property="Button.Background" Value="Red" /> <!-- Only inner buttons -->
        </Style>            
    </Window.Resources>

    <Grid Style="{StaticResource MyStyle}">
        <Button Content="Go" Margin="29,36,385,239" />
    </Grid>
</Window>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一个人的旅程 2024-10-24 04:33:49

为了实现您想要的目标,我认为您必须在 Style.Resources 中定义内部 Style 。这将使 Grid 中的所有 Button 选择“内部”Style,除非它们明确使用另一个 Style >

<Window.Resources>
    <Style x:Key="MyStyle">
        <Style.Resources>
            <!-- Only inner buttons -->
            <Style TargetType="Button">
                <Setter Property="Background" Value="Red" />
            </Style>
        </Style.Resources>
    </Style>
</Window.Resources>
<Grid Style="{StaticResource MyStyle}">
    <Button Content="Go" Margin="29,36,385,239" />
</Grid>

由于 Button.Background 不是附加属性(与 TextBlock.Foreground 不同),因此 Background 将不会应用于 < 网格中的code>按钮。

但至于“为什么网格会选择背景”我无法告诉你。对我来说这似乎是一个错误。 ButtonBackground 继承自 ControlGridBackground 继承自 ControlPanel 来看,该值不应该由 Grid 使用,但我可能会丢失一些东西

此外,如果您尝试直接在 Grid 上设置 Button.Background

错误 MC3015:附加属性
'Button.Background' 未定义
“Grid”或其基类之一。

To achieve what you're after I think you're gonna have to define the inner Styles within Style.Resources. This will make all the Buttons in the Grid pick up the "inner" Style unless they explictly use another Style

<Window.Resources>
    <Style x:Key="MyStyle">
        <Style.Resources>
            <!-- Only inner buttons -->
            <Style TargetType="Button">
                <Setter Property="Background" Value="Red" />
            </Style>
        </Style.Resources>
    </Style>
</Window.Resources>
<Grid Style="{StaticResource MyStyle}">
    <Button Content="Go" Margin="29,36,385,239" />
</Grid>

Since Button.Background isn't an attached property (unlike e.g. TextBlock.Foreground), the Background won't be applied to the Buttons in the Grid.

But as for the "Why does the Grid pick up the Background" I couldn't tell you. It seems like a bug to me. Background for a Button is inherited from Control and Background for a Grid is inherited from Panel so as far as I can see, that value shouldn't be used by the Grid but I might be missing something

Also, you'll get the following error if you try to set Button.Background directly on a Grid

error MC3015: The attached property
'Button.Background' is not defined on
'Grid' or one of its base classes.

吻泪 2024-10-24 04:33:49

不能将TargetType设置为button,以便该样式仅应用于Button吗?

<Style x:Key="MyStyle" TargetType="Button">
  <Setter Property="Background" Value="Red" />
</Style>

Can't you set the TargetType to button so that this style is only applied to a Button?

<Style x:Key="MyStyle" TargetType="Button">
  <Setter Property="Background" Value="Red" />
</Style>
不再见 2024-10-24 04:33:49

遗憾的是,风格并非如此。如果你有一个已知的子集合,你可以用类似的东西来作弊(丑陋):

<Setter Property="{Binding RelativeSource={RelativeSource Self} Path=Children[0].Background}" Value="Red" />

当然,这只有在你知道子索引的情况下才有效,而且它非常脆弱。我不确定它是否适合您的情况,因为您说您必须将样式应用到网格,所以我猜测网格内容是动态生成的。

Sadly, styles don't work like that. If you have a known child collection, you can cheat with something like (ugly):

<Setter Property="{Binding RelativeSource={RelativeSource Self} Path=Children[0].Background}" Value="Red" />

Of course, this only works if you know the children indices, and it's pretty fragile. I'm not sure that it'll work for your case b/c you said that you must apply the style to the grid, so I'm guessing the grid contents are getting dynamically generated.

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