是否可以在XAML中设置有选择地影响控件的样式?

The*_*rff 9 .net wpf xaml styles

<Window.Resources>我定义了以下样式:

    <Style x:Key="textBlockStyle" TargetType="TextBlock">
        <Setter Property="Margin" Value="5,0,5,0"/>
    </Style>
Run Code Online (Sandbox Code Playgroud)

我已经定义了一些网格,我有四个TextBlocks:

    <WrapPanel>
        <TextBlock Style="{StaticResource textBlockStyle}">Server</TextBlock>
        <TextBlock Style="{StaticResource textBlockStyle}">IP</TextBlock>
        <TextBlock Style="{StaticResource textBlockStyle}">Port</TextBlock>
        <TextBlock Style="{StaticResource textBlockStyle}">Status</TextBlock>
    </WrapPanel>
Run Code Online (Sandbox Code Playgroud)

问题:我需要参考textBlockStyle四次.

问题:是否可以在内部WrapPanel或其他地方设置该样式一次而不重复对样式的引用?

也许是这样的:

    <WrapPanel Style="{StaticResource textBlockStyle}">
        <TextBlock>Server</TextBlock>
        <TextBlock>IP</TextBlock>
        <TextBlock>Port</TextBlock>
        <TextBlock>Status</TextBlock>
    </WrapPanel>
Run Code Online (Sandbox Code Playgroud)

我不是在寻找全球解决方案!我可以删除该x:Key="textBlockStyle"属性,但这会影响窗口中的所有 属性TextBlocks.我需要一个更具选择性的机制,但没有那个丑陋的代码重复.

Ken*_*art 17

您有几个选项,按照它们的缩放程度排列.

选项1:在较低级别定义没有键的样式

您可以将资源粘贴到该WrapPanel级别,以便它只影响其中的控件WrapPanel:

<WrapPanel>
    <WrapPanel.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="5,0,5,0"/>
        </Style>
    </WrapPanel.Resources>

    <!-- TextBlocks here -->
</WrapPanel>
Run Code Online (Sandbox Code Playgroud)

注意缺少钥匙.这Style将适用于所有TextBlocks内WrapPanel.

选项2:使用键定义样式,而不是在较低级别定义样式

如果Style使用键定义更高级别,则可以Style在没有键的情况下在较低级别定义另一个级别,并将其Style基于较高级别的级别:

<Window>
    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="textBlockStyle">
            <Setter Property="Margin" Value="5,0,5,0"/>
        </Style>
    </Window.Resources>

    <WrapPanel>
        <WrapPanel.Resources>
            <Style TargetType="TextBlock" BasedOn="{StaticResource textBlockStyle"/>
        </WrapPanel.Resources>

        <!-- TextBlocks here -->
    </WrapPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

这导致Style自动应用于TextBlocks内部WrapPanel,但不在其外部.此外,您不会复制它们的详细信息Style- 它们存储在更高级别.

选项3:将样式放在ResourceDictionary中并有选择地合并它

最后,您可以将Styles放在一个单独的位置,ResourceDictionary并有选择地将该字典合并到控件的Resources集合中:

<!-- TextBlockStyles.xaml -->
<ResourceDictionary>
    <Style TargetType="TextBlock">
        <Setter Property="Margin" Value="5,0,5,0"/>
    </Style>
</ResourceDictionary>

<!-- Window.xaml -->
<Window>
    <WrapPanel>
        <WrapPanel.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="TextBlockStyles.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </WrapPanel.Resources>
    </WrapPanel>
</Window>

<!-- Alternative Window.xaml if you have only one RD to merge in -->
<Window>
    <WrapPanel>
        <WrapPanel.Resources>
            <ResourceDictionary Source="TextBlockStyles.xaml"/>
        </WrapPanel.Resources>
    </WrapPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

现在,您可以根据需要在单独的词典中定义任意数量的样式集,然后有选择地将它们应用于元素树.