是否有可能设置在XAML的风格,选择性地影响控制?
在< Window.Resources>
我已经定义了以下样式:
In <Window.Resources>
I have defined following style:
<Style x:Key="textBlockStyle" TargetType="TextBlock">
<Setter Property="Margin" Value="5,0,5,0"/>
</Style>
我已经定义了一些网格,我有四个的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>
问题:我需要引用 textBlockStyle
四次
问:是否有可能设置的风格只有一次在 WrapPanel中
或其他地方不重复引用样式
Question: Is it possible to set that style just once at in WrapPanel
or somewhere else without repeating the reference to the style?
也许是这样的:
<WrapPanel Style="{StaticResource textBlockStyle}">
<TextBlock>Server</TextBlock>
<TextBlock>IP</TextBlock>
<TextBlock>Port</TextBlock>
<TextBlock>Status</TextBlock>
</WrapPanel>
我不是在寻找一个全球性的解决方案我可以删除 X:!关键=textBlockStyle
属性,但是会影响到所有的的TextBlocks
在窗口中。我需要一个更具有选择性的机制,但是没有说难听code复制。
I am not searching for a global solution! I could delete that x:Key="textBlockStyle"
property, but that would affect all TextBlocks
in the Window. I need a more selective mechanism, but without that ugly code duplication.
您有几种选择,$ P $这里psented为了对他们如何进行扩展。
You have several options, presented here in order of how well they scale.
选项1:定义样式,而不在一个较低的水平的关键
您可以贴在 WrapPanel中
级别的资源,使其只影响内部控制 WrapPanel中
:
You can stick the resource at the WrapPanel
level so that it only affects controls inside that WrapPanel
:
<WrapPanel>
<WrapPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="5,0,5,0"/>
</Style>
</WrapPanel.Resources>
<!-- TextBlocks here -->
</WrapPanel>
注意缺少关键的。这风格
将适用于所有的TextBlock
秒内的 WrapPanel中
Notice the lack of key. This Style
will apply to all TextBlock
s within the WrapPanel
.
选项2:定义风格的关键,再没有在一个较低的水平
如果您定义的风格
在更高层次上带有一键,你就可以定义另一个风格
在不用钥匙较低的水平,而基地风格
在较高的水平之一:
If you define the Style
at a higher level with a key, you can then define another Style
at a lower level without a key, and base that Style
on the higher level one:
<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>
这导致了风格
S内部的 WrapPanel中的TextBlock
/ code>,而不是外部。另外,你不要复制风格
的细节 - 它们被存储在一个较高的水平。
This results in a Style
being automatically applied to TextBlock
s inside the WrapPanel
, but not outside it. Also, you don't duplicate the details of the Style
- they are stored at a higher level.
方法3:将在ResourceDictionary中的样式,并选择将其合并
最后,你可以把你的风格
S在一个单独的的ResourceDictionary
并有选择地合并词典到控制的资源系列:
Finally, you can place your Style
s in a separate ResourceDictionary
and selectively merge that dictionary into a control's Resources
collection:
<!-- 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>
现在,你可以在不同的字典定义,只要你喜欢尽可能多样式集,然后选择性地将它们应用到你的元素树。
Now you can have as many style sets defined in separate dictionaries as you like, and then selectively apply them to your element tree.