René Stalder
René Stalder

Reputation: 2546

Mixing general WPF styles with ResourceDictionary

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.

Upvotes: 8

Views: 10009

Answers (2)

NeroBrain
NeroBrain

Reputation: 71

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>  

Upvotes: 5

brunnerh
brunnerh

Reputation: 184296

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>

Upvotes: 21

Related Questions