从 wpf 中的单独文件加载控件样式 [英] Load control style from separate file in wpf
问题描述
我在 Windows.Resources 中添加了以下样式
I have the following style added to my Windows.Resources
<Window.Resources>
...
<!--A Style that extends the previous TextBlock Style-->
<!--This is a "named style" with an x:Key of TitleText-->
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="TitleText">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Foreground">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.0" Color="#90DDDD" />
<GradientStop Offset="1.0" Color="#5BFFFF" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
...
</Window.Resources>
我的 xaml 代码中有很多这些样式,我想将每个组件样式保存到一个额外的文件(而不是外部文件)中.例如,与 TextBlocks 相关的所有样式都应该在一个名为 <代码>TextBlockStyles.xaml
I have a lot of those styles in my xaml code and I would like to save each component style to an extra file (not an external file).. for example all the styles related to TextBlocks should be in a file called TextBlockStyles.xaml
我将如何在 wpf 中执行此操作?
How would I do this in wpf?
如何链接项目中的样式?
How do I link the style in my project ?
提前致谢
推荐答案
您使用合并的资源词典
在你的 app.xaml 中你会使用
In you app.xaml you would use
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
... other dictionaries here
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
或直接进入 UserControl 将是
or directly into a UserControl would be
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
... other dictionaries here
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
如果文件在同一个程序集中并且位于项目,或者 Source="Styles\TextBlockStyles.xaml"
如果您将资源字典放入文件夹 Styles
.
You can shorten Source="..."
to just Source="TextBlockStyles.xaml"
if the file is in the same assembly and in the root of the project, or alternatively Source="Styles\TextBlockStyles.xaml"
if you put the resource dictionary into the folder Styles
.
这篇关于从 wpf 中的单独文件加载控件样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!