为堆栈面板中的所有文本块设置样式 [英] Set style for all textblocks in a stack panel

查看:26
本文介绍了为堆栈面板中的所有文本块设置样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个不同的、不同的堆栈面板(我们将它们称为 SPA 和 SPB),每个面板都有 10 个文本块作为子元素.SPA 中的所有文本块应使用一种样式,而 SPB 中的所有文本块应使用另一种样式.实现此目的的一种方法是在资源中声明两种样式,然后将 Style="style1" 附加到第一个堆栈面板中的所有 10 个文本块,并附加 Style="style2" 到第二个中的所有 10.但是,似乎应该有一些简单的方法可以将样式附加到堆栈面板本身,以某种方式告诉堆栈面板将其应用于作为文本块的所有子元素.有没有办法做到这一点?

Let's say I have two different, distinct stack panels (we'll call them SPA and SPB), each with 10 textblocks as child elements. All the textblocks in SPA should use one style, and all the textblocks in SPB should use another. One way to accomplish this would be to declare the two styles in Resources, and then append Style="style1" to all 10 textblocks in the first stack panel, and append Style="style2" to all 10 in the second one. However, it seems like there should be some easy way to append a style to the stackpanel itself that somehow tells the stackpanel to apply it to all child elements that are textblocks. Is there anyway to do this?

我自然而然地寻找这个解决方案的原因是因为这正是您在 HTML 中使用 CSS 做同样事情的方式,我希望会有一个与 XAML 类似的带有样式的功能.

The reason I naturally look for this solution is because this is exactly how you do the same sort of thing in HTML with CSS, and I was hoping there would be a similar feature to XAML with styling.

谢谢!

附言我正在使用 Silverlight,但我猜测我的情况和任何解决方案(如果有的话)一般适用于 XAML/WPF.

P.S. I am working with Silverlight, but I'm guessing my situation and whatever solution (if there is one) applies to XAML/WPF in general.

推荐答案

在您的主容器的资源部分,将您的样式与 x:Key 属性和 TextBlock 的目标类型放在一起.然后在每个 StackPanel 的每个资源部分中,您可以放置​​一个样式,其中 BasedOn 属性设置为主样式的键(不要忘记使用 StaticResource 绑定,不只是键的名称),然后说 TargetType="{x:Type TextBlock}" 并结束标记.这应该将样式带入 StackPanel 并设置所有 TextBlock 的样式.

In the resources section for your main container put your style with a x:Key attribute and a target type of TextBlock. Then in each resources section for each StackPanel you can put a style where the BasedOn attribute is set to the key of your main style (don't forget to use StaticResource binding, not just the name of the key) and then say TargetType="{x:Type TextBlock}" and end the tag. this should bring the style into the StackPanel and style all of your TextBlocks.

<Window ...>
    <Window.Resources>
        <Style x:Key="tbstyle" TargetType="{x:Type TextBlock}">
            <!-- put setters here -->
        </Style>
    </Window.Resources>
    <StackPanel name="SPA">
        <StackPanel.Resources>
            <Style BasedOn="{StaticResource tbstyle}" TargetType="{x:Type TextBlock}" />
        </StackPanel.Resources>
        <TextBlock ... />
        <TextBlock ... />
        <TextBlock ... />
        <TextBlock ... />
        <TextBlock ... />
    </Stackpanel>
    <StackPanel name="SPB">
        <StackPanel.Resources>
            <Style BasedOn="{StaticResource tbstyle}" TargetType="{x:Type TextBlock}" />
        </StackPanel.Resources>
        <TextBlock ... />
        <TextBlock ... />
        <TextBlock ... />
        <TextBlock ... />
        <TextBlock ... />
    </StackPanel>
</Window>

这篇关于为堆栈面板中的所有文本块设置样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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