[英]Loading WPF Style from Resource File
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 WPF自定义控件库类型的WpfTestControls
WpfTestApp of type WPF Application Library which has reference to 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
提前致谢
Please answer few things for me... 请为我回答几件事......
WpfTestApp
have a reference to both "WPF Custom Control Library" and "WpfTestControls" assemblies? WpfTestApp
是否同时引用“WPF自定义控件库”和“WpfTestControls”程序集? If you add that reference(s), the resources should load correctly. 如果添加该引用,则应正确加载资源。
My Steps... 我的步骤......
TextBoxStyle.xaml TextBoxStyle.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 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
. 我有主要的starup项目“MyWPFTestApp”,它有对
ThemesLibray
程序集引用。 In that the window has ThemesLibrary
resources merged this way.... 在那个窗口中
ThemesLibrary
这种方式合并了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时,我看到带有绿色TextBox的Window。
主要是:确保将资源字典的构建操作设置为资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.