wpf 为堆栈面板中的所有文本块设置样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4877515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:33:05  来源:igfitidea点击:

Set style for all textblocks in a stack panel

wpfxamlstyles

提问by JoeCool

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?

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

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.

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

Thanks!

谢谢!

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.

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

回答by Scott M.

In the resources section for your main container put your style with a x:Keyattribute and a target type of TextBlock. Then in each resources section for each StackPanelyou can put a style where the BasedOnattribute 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.

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

<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>

回答by decyclone

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Margin"
                    Value="5" />
        </Style>
    </StackPanel.Resources>
    <TextBlock Text="Text" />
    <TextBlock Text="Text" />
    <TextBlock Text="Text" />
    <TextBlock Text="Text" />
</StackPanel>

回答by Pavlo Glazkov

You can accomplish this by overriding default text block style in the Resources of each stack panel:

您可以通过覆盖每个堆栈面板的资源中的默认文本块样式来实现此目的:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background"
                    Value="Red"/>
        </Style>
    </StackPanel.Resources>

    <TextBlock .../>
    <TextBlock .../>
    <TextBlock .../>
    <TextBlock .../>
</StackPanel>

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background"
                    Value="Green"/>
        </Style>                
    </StackPanel.Resources>

    <TextBlock .../>
    <TextBlock .../>
    <TextBlock .../>
    <TextBlock .../>
</StackPanel>

回答by ShaileshDev

I found one good solution for it here. Below is the sample code -

我在这里找到了一个很好的解决方案。以下是示例代码 -

<Window x:Class="WpfTutorialSamples.Styles.WindowWideStyleSample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WindowWideStyleSample" Height="200" Width="300">
<Window.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Gray" />
        <Setter Property="FontSize" Value="24" />
    </Style>
</Window.Resources>
<StackPanel Margin="10">
    <TextBlock>Header 1</TextBlock>
    <TextBlock>Header 2</TextBlock>
    <TextBlock Foreground="Blue">Header 3</TextBlock>
</StackPanel>

I hope this would help.

我希望这会有所帮助。