从资源文件加载WPF样式
我试图从其他文件加载WPF样式,实际上是从WPF自定义控件库加载的 但我无法加载这里是我的解决方案 该解决方案包含两个项目从资源文件加载WPF样式,wpf,xaml,custom-controls,resourcedictionary,generic.xaml,Wpf,Xaml,Custom Controls,Resourcedictionary,Generic.xaml,我试图从其他文件加载WPF样式,实际上是从WPF自定义控件库加载的 但我无法加载这里是我的解决方案 该解决方案包含两个项目 WPF自定义控件库类型的WpfTestControls WPF应用程序库类型的WpfTestApp,它引用了WpfTestControls WPF应用程序库中的MainWindow.xaml <Window.Resources> <Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
<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>
<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>
<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>
但我仍然无法加载样式。
如果我使用的是Generic.xaml,而不是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>
我做错了什么?
提前谢谢请帮我回答几件事
WpfTestApp
是否同时引用了“WPF自定义控件库”和“WpfTestControls”程序集 <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>
<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>
当我启动MyWPFTestApp时,我看到一个带有绿色文本框的窗口。主要内容是:确保资源字典的生成操作设置为Resource。您好,可能我写错了,但解决方案只包含两个项目WPF自定义控件库类型的WpfTestControls,和Wpf应用程序类型的WpfTestApp,它引用了两个WpfTestControls。您是对的,但据我所知,它应该与以前的解决方案一起工作。这是我目前将采用的解决办法
<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>
<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>