开发者

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.

The solution contains two projects

  1. WpfTestControls of Type WPF Custom Control Library

  2. WpfTestApp of type WPF Application Library which has reference to WpfTestControls

MainWindow.xaml from WPF Application Library

<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

<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

<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

[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

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

  1. Is "WPF Custom Control Library" assembly same as "WpfTestControls" assembly?
  2. If not, then does "WPF Custom Control Library" have a reference to the "WpfTestControls" assembly?
  3. Does your WpfTestApp have a reference to both "WPF Custom Control Library" and "WpfTestControls" assemblies?

If you add that reference(s), the resources should load correctly.

My Steps...

  1. Add a "WPF Custom Control Library" say "ThemesLibray"
  2. In this add two resource dictionaries under "Themes" folder

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>
  1. I have main starup project "MyWPFTestApp" that has assembly reference to ThemesLibray. In that the window has ThemesLibrary 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>
    

When I launch MyWPFTestApp, I see the Window with green TextBox.


Main thing is : Make sure you have Build Action of your Resource Dictionary set to Resource.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜