从资源文件加载 WPF 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7761681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading WPF Style from Resource File
提问by Robob
I am trying to load WPF Style from other file actually from WPF Custom Control Library but i am failing to load here is my solution.
我正在尝试从 WPF 自定义控件库中的其他文件加载 WPF 样式,但我无法加载这里是我的解决方案。
The solution contains two projects
解决方案包含两个项目
WpfTestControls of Type WPF Custom Control Library
WpfTestApp of type WPF Application Library which has reference to WpfTestControls
WPF 自定义控件库类型的 WpfTestControls
类型为 WPF 应用程序库的 WpfTestApp,其中引用了 WpfTestControls
MainWindow.xaml from WPF Application Library
WPF 应用程序库中的 MainWindow.xaml
<Window.Resources>
<Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Green"/>
</Style>
</Window.Resources>
<Grid>
<TextBox Height="50px" Width="100px" Style="{DynamicResource TempStyle}"/>
</Grid>
Generic.xaml from WPF Custom Control Library
来自 WPF 自定义控件库的 Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfTestControls;component/TextBoxStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
TextBoxStyle.xaml from WPF Custom Control Library
WPF 自定义控件库中的 TextBoxStyle.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Green"/>
</Style>
My AssemblyInfo.cs file contains the following
我的 AssemblyInfo.cs 文件包含以下内容
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries))]
But still i am failing to load the Style. If i am using the not using the Generic.xaml everything work fine for example the following code works as expected
但我仍然无法加载样式。如果我使用不使用 Generic.xaml 一切正常,例如以下代码按预期工作
<Window x:Class="WpfTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Green"/>
</Style>
</Window.Resources>
<Grid>
<TextBox Height="50px" Width="100px" Style="{StaticResource TempStyle}"/>
</Grid>
What am i doing wrong ? Thanks in advance
我究竟做错了什么 ?提前致谢
回答by WPF-it
Please answer few things for me...
请帮我回答几件事...
- Is "WPF Custom Control Library" assembly same as "WpfTestControls" assembly?
- If not, then does "WPF Custom Control Library" have a reference to the "WpfTestControls" assembly?
- Does your
WpfTestApp
have a reference to both "WPF Custom Control Library" and "WpfTestControls" assemblies?
- “WPF 自定义控件库”程序集与“WpfTestControls”程序集相同吗?
- 如果没有,那么“WPF 自定义控件库”是否引用了“WpfTestControls”程序集?
- 您是否
WpfTestApp
同时引用了“WPF 自定义控件库”和“WpfTestControls”程序集?
If you add that reference(s), the resources should load correctly.
如果您添加该引用,资源应正确加载。
My Steps...
我的步骤...
- Add a "WPF Custom Control Library" say "ThemesLibray"
- In this add two resource dictionaries under "Themes" folder
- 添加“WPF 自定义控件库”说“ThemesLibray”
- 在这个“Themes”文件夹下添加两个资源字典
TextBoxStyle.xaml
文本框样式.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="GreenTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Green"/>
</Style>
</ResourceDictionary>
Generic.xaml
通用文件
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TextBoxStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
I have main starup project "MyWPFTestApp" that has assembly reference to
ThemesLibray
. In that the window hasThemesLibrary
resources merged this way....<Window x:Class="MyWPFTestApp.Window7" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window7" Height="300" Width="300"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/ThemseLibrary;component/Themes/Generic.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid> <TextBox Style="{StaticResource GreenTextBoxStyle}"/> </Grid> </Window>
我有主要的启动项目“MyWPFTestApp”,它具有对
ThemesLibray
. 因为该窗口以ThemesLibrary
这种方式合并了资源....<Window x:Class="MyWPFTestApp.Window7" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window7" Height="300" Width="300"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/ThemseLibrary;component/Themes/Generic.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid> <TextBox Style="{StaticResource GreenTextBoxStyle}"/> </Grid> </Window>
When I launch MyWPFTestApp, I see the Window with green TextBox.
当我启动 MyWPFTestApp 时,我看到带有绿色文本框的窗口。
回答by AnjumSKhan
Main thing is : Make sure you have Build Action of your Resource Dictionary set to Resource.
主要的是:确保您的资源字典的构建操作设置为资源。