C# 从wpf中的单独文件加载控件样式
我的Windows中添加了以下样式。参考资料C# 从wpf中的单独文件加载控件样式,c#,.net,wpf,C#,.net,Wpf,我的Windows中添加了以下样式。参考资料 <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="
<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的文件中
在wpf中我将如何执行此操作
如何在项目中链接样式
提前感谢您使用合并的资源词典
在app.xaml中,您将使用
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
... other dictionaries here
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
... 这里还有其他字典吗
或者直接进入用户控件
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
... other dictionaries here
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
... 这里还有其他字典吗
如果文件位于同一程序集中且位于项目根目录中,则可以将Source=“…”
缩短为仅Source=“TextBlockStyles.xaml”
,或者Source=“style\TextBlockStyles.xaml”
如果将资源字典放入文件夹style
中,则您正在查找动态资源。最好的方法是在参考资料中加载字典并加上边框。应用程序或控件页上的任何一个。
这是一个很好的例子
this.Resources.MergedDictionaries.Add(Smyresourcedictionary) 用例:您有一个名为MyView.xaml
的带有按钮的用户控件。您希望使用外部XAML文件设置按钮的样式
在MyView.xaml
中:
<User Control ...namespaces...>
<UserControl.Resources>
<ResourceDictionary>
...converters...
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
...the rest of the control...
</UserControl>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MSDNSample">
<Style x:Key="FooButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Pink" />
</Style>
</ResourceDictionary>
回到MyView.xaml
(“控件的其余部分”):
你好,世界
在解决方案资源管理器中右键单击项目选择添加,然后单击资源字典…
选择名称并添加到项目中。
打开App.xaml
将此代码添加到应用程序标记中
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="YourStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
在YourStyle.xaml中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:APPNAME">
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Pink" />
</Style>
</ResourceDictionary>
简单地说,转到您的窗口(例如:MaindWindow.xaml
),您希望在其中包含外部文件中的资源,并使用原则引用该文件:
从上面的Merged/BrushResources.xaml
是资源文件的位置,位于名为Merged
的文件夹下
现在,如果您想知道外部文件中的声明语法应该是什么,请检查以下内容:
如果要使资源通过所有应用程序可用(在所有窗口中可见),请在App.xaml
resources部分中声明
那么名为TextBlockStyles.xaml
的文件看起来像什么呢?@TheMuffinMan:一个例子。(在本例中称为Dictionary1.xaml
)不要忘记添加对定义样式的dll的引用。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="YourStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:APPNAME">
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Pink" />
</Style>
</ResourceDictionary>