Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing general WPF styles with ResourceDictionary

Tags:

styles

wpf

xaml

I came from web development and WinForms to WPF and maybe I didn't get the concept yet. I'm able to define general styles for my Application in the app.xaml. For example I defined the style for all my ribbon controls in this file.

Then I tried Microsoft Blend and came across ResourceDictionary, which is somekind of Resource File .resx I knew from WinForms.

But as I see it's not possible to mix these two concepts. For example following xaml code will not work because ResourceDictionary have to be the only child.

<Application x:Class="Wpf.MyApplication.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
             StartupUri="MyMainWindow.xaml">
    <Application.Resources>
        <!-- Resources scoped at the Application level should be defined here. -->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles/RibbonStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <BitmapImage x:Key="IconDokumentNeu" >Images/NewDocument_32x32.png</BitmapImage>
      <SolidColorBrush x:Key="LightGrayBrushKey">WhiteSmoke</SolidColorBrush>
    </ResourceDictionary>
    <Style TargetType="{x:Type ribbon:RibbonWindow}">
        <Setter Property="Icon" Value="../time2_32.png" />
        <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
    </Style>
    </Application.Resources>
</Application>

It seems I didn't really get the concept. Maybe you can help me, why this is not possible and how I can use general styles next to ResourceDictionary.

like image 301
René Stalder Avatar asked Jan 13 '12 08:01

René Stalder


People also ask

What is resources dictionary in WPF?

Here WPF provide an easy way to manage these styles and other reusable resources which can be used across WPF windows and across application. This is called Resources Dictionary. We can define the styles in WPF XAML files and can manage all our useful styles for a particular application in a resource dictionary file.

What is a merged resource dictionary in WPF?

Windows Presentation Foundation (WPF) resources support a merged resource dictionary feature. This feature provides a way to define the resources portion of a WPF application outside of the compiled XAML application. Resources can then be shared across applications and are also more conveniently isolated for localization.

How do I use string resources in XAML?

Declarative use of string resource from StringResources.xaml resource dictionary --> <TextBox DockPanel.Dock="Top" Text=" {StaticResource localizedMessage}" /> Use the string resource from code-behind, using code like the following. Localize the application. For more information, see Localize an Application.

How to reference a style inside WPF XAML code?

If you don’t want a resource to be used everywhere in your application if you style inside the resource dictionary a name. Then you can reference the style inside your WPF XAML code. Enjoy!


2 Answers

You already have resources defined "next to" the dictionary, one image and one brush.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- Dictionaries from file here -->
        </ResourceDictionary.MergedDictionaries>

        <!-- Other resources here -->
    </ResourceDictionary>
</Application.Resources>
like image 141
H.B. Avatar answered Oct 04 '22 13:10

H.B.


Just include the {x:type} style in the resource dictionary

  <ResourceDictionary>
           <ResourceDictionary.MergedDictionaries> 
                   <!-- Dictionaries from file here -->  
           </ResourceDictionary.MergedDictionaries>    
           <Style TargetType="{x:Type ribbon:RibbonWindow}">         
               <Setter Property="Icon" Value="../time2_32.png" />         
               <Setter Property="TextOptions.TextFormattingMode" Value="Display" />  
           </Style> 
     </ResourceDictionary>  
like image 40
NeroBrain Avatar answered Oct 04 '22 12:10

NeroBrain