2011-10-13 150 views
5

我想从WPF自定义控件库 实际上从其他文件加载WPF样式,但我无法加载在这里是我的解决方案。从资源文件加载WPF样式

该溶液包含两个项目

  1. WpfTestControls类型WPF定制控件库

  2. WpfTestApp类型的WPF应用程序图书馆,其具有参考WpfTestControls

MainWindow.xaml来自WPF应用程序库

<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从WPF自定义控件库

<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从WPF自定义控件库

<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> 

我的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))] 

但我仍无法加载样式。 如果我使用的是不使用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> 

我到底做错了什么? 在此先感谢

回答

5

请为我解答一些事情......

  1. 是“WPF自定义控件库”为“WpfTestControls”议会会议一样吗?
  2. 如果不是,那么“WPF自定义控件库”是否有对“WpfTestControls”程序集的引用?
  3. 您的WpfTestApp是否对“WPF自定义控件库”和“WpfTestControls”程序集有引用?

如果添加该引用,则应正确加载资源。

我的步骤...

  1. 添加 “WPF自定义控件库” 说 “ThemesLibray”
  2. 在这种添加在 “主题” 文件夹

两个资源字典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

<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> 
  • 我有主要的Starup项目 “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> 
    
  • 当我启动MyWPFTestApp,我看到绿色的文本框的窗口。

    +0

    嗨也许我写错了,但解决方案只包含两个项目类型WPF自定义控件库WpfTestControls和类型Wpf应用程序的WpfTestApp具有引用两个WpfTestControls。 – Robob

    +0

    正确,所以看到我上面的编辑... –

    +0

    你是对的,但据我了解,它应该与以前的解决方案。这是我现在要采用的解决方法 – Robob

    1

    主要事情是:确保您的资源字典的生成操作设置为资源。