【发布时间】:2018-12-02 12:10:37
【问题描述】:
我使用如下资源将 Grid 控件设置为表格标题:
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Padding" Value="5,10" />
<Setter Property="Foreground" Value="{StaticResource ForegroundDarkBrush}" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
<Style TargetType="Border">
<Setter Property="BorderThickness" Value="0.0,1.0,0.0,0" />
<Setter Property="BorderBrush" Value="{StaticResource ForegroundDarkBrush}" />
<Setter Property="Background" Value="{StaticResource BackgroundLightBrush}" />
</Style>
</Grid.Resources>
问题是,我需要将这些资源应用到我的应用中的多个位置,这会导致代码重复。
我想知道这是否可以将资源存储在我的App.xaml 中并通过密钥或类似的方式使用它们?像这样:
<Resources Key="MyResourceSet">
<Style>
[..]
</Style>
</Resources>
<Grid Resource="MyResourceSet">
[...]
</Grid>
【问题讨论】:
-
你试过了吗?你绝对可以。查找资源时,它从第一个可用资源开始向上查找,因此 App.xaml 是它查找资源的最后一个位置。示例... TextBlock、Button、Grid、Window、App... 如果使用 TextBlock 中的资源,它会先在那里向外寻找,就像示例一样。
-
@MichaelPuckettII 我不能通过键/名称使用它们。这是不可能的。
-
有可能,我一直这样做。
-
我相信您现在可能会问一些不同的问题,因为我正在摸不着头脑。您想将资源字典应用于您的各个控件。你也可以这样做,但不是你期望的那样,我不相信。
-
@MichaelPuckettII 非常感谢您的努力! :-)
标签: c# wpf xaml resources styles