Loading WPF style from resource file

I am trying to load the WPF style from another file from the WPF user control library but I cannot load here, this is my solution.

The solution contains two projects

  • WpfTestControls type WPF user control library

  • WpfTestApp of type WPF Application Library, which refers to WpfTestControls

MainWindow.xaml from the 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 User 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 User Management 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 can’t load the style. If I use do not use Generic.xaml, everything works 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

+5
2

, ...

  • WPF , "WpfTestControls" ?
  • , " WPF" "WpfTestControls" ?
  • WpfTestApp " WPF" "WpfTestControls" ?

, .

...

  • "WPF Custom Control Library", , "ThemesLibray"
  • ""

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. "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, TextBox.

+5

: , Action .

+1

All Articles