WPF:按键解决嵌套样式 [英] WPF: Address nested styles by key

查看:37
本文介绍了WPF:按键解决嵌套样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当复杂的 WPF UserControl,它需要很多自定义样式,以及相同控件类型的几种不同样式.其他任何地方都没有使用相同的样式.

I have a fairly complex WPF UserControl that needs a lot of custom styling, and several different styles for the same control types. The same styles are not used in any other places.

我想使用嵌套样式(使用 Style.Resources)作为一种命名空间机制,如下所示:

I would like to use nested styles (using Style.Resources) as a sort of namespacing mechanism as follows:

示例用户控件:

<UserControl Style="{StaticResource AwesomeControl}>
    <Grid>
        <Button Style="{StaticResource ButtonA}"/>
        <Button Style="{StaticResource ButtonB}"/>
    </Grid>
</UserControl>

想要如何定义我的风格:

<ResourceDictionary>
    <Style TargetType="UserControl" x:Key="AwesomeControl">
        <Style.Resources>
            <Style TargetType="Button" x:Key="ButtonA"> </Style>
            <Style TargetType="Button" x:Key="ButtonB"> </Style>
        </Style.Resources>
    </Style>
</ResourceDictionary>

然而,这不起作用.据我所知,似乎不可能通过它们的键来解决嵌套样式.(我已经搜索了很多,但找不到一个这样的例子.)

However, this does not work. From what I can tell, it does not seem possible to address nested styles by their key. (I have searched around a lot, but cannot find a single example doing something like this.)

通过删除样式的嵌套,将它们全部保持在顶层,我可以使其轻松工作.但是,然后我必须将它们的键更改为 AwesomeControlButtonA 等,以将它们与应用程序的其他部分区分开来

I can make it work easily by removing the nesting of styles, keeping them all at top level. However, then I have to change their keys to something like AwesomeControlButtonA, etc. to distinguish them from other parts of the application

这对我来说似乎并不理想.

That does not seem ideal to me.

所以我的问题是:

我正在尝试使用上面的代码可能吗?如果没有,我是否可以使用其他命名空间方法来防止像 AwesomeControlButtonA 这样笨拙的键?

推荐答案

也许 DynamicResource 可以解决你的问题

Maybe DynamicResource could solve your problem

<Grid>
    <Button Style="{DynamicResource ButtonA}"/>
    <Button Style="{DynamicResource ButtonB}"/>
</Grid>

投反对票后:

<Grid>
    <Grid.Resources>
        <Style x:Key="AW" TargetType="UserControl">
            <Style.Resources>
                <Style TargetType="Button" x:Key="AB">
                    <Setter Property="Background" Value="Red" />
                </Style>
                <Style TargetType="Button" x:Key="BB">
                    <Setter Property="Background" Value="Yellow" />
                </Style>
            </Style.Resources>
        </Style>
        <Style x:Key="AR" TargetType="UserControl">
            <Style.Resources>
                <Style TargetType="Button" x:Key="AB">
                    <Setter Property="Background" Value="Green" />
                </Style>
                <Style TargetType="Button" x:Key="BB">
                    <Setter Property="Background" Value="Blue" />
                </Style>
            </Style.Resources>
        </Style>
    </Grid.Resources>

    <StackPanel>
        <UserControl Style="{StaticResource AW}">
            <StackPanel>
                <Button Content="A" Style="{DynamicResource AB}" />
                <Button Content="A" Style="{DynamicResource BB}" />
            </StackPanel>
        </UserControl>
        <UserControl Style="{StaticResource AR}">
            <StackPanel>
                <Button Content="A" Style="{DynamicResource AB}" />
                <Button Content="A" Style="{DynamicResource BB}" />
            </StackPanel>
        </UserControl>
    </StackPanel>
</Grid>

我认为这是一个解决方案......感谢反对票.批评总是更好:)

I think it's a solution.. and thx for the down vote. Critict is always better :)

这篇关于WPF:按键解决嵌套样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆