将常规WPF样式与ResourceDictionary混合使用

Ren*_*der 6 wpf xaml styles

我来自Web开发和WinForms到WPF,也许我还没有得到这个概念.我可以在app.xaml中为我的应用程序定义一般样式.例如,我为此文件中的所有功能区控件定义了样式.

然后我尝试了Microsoft Blend并遇到了ResourceDictionary,它是我从WinForms知道的资源文件.resx的某种方式.

但是我认为不可能将这两个概念混合在一起.例如,以下xaml代码将无法工作,因为ResourceDictionary必须是唯一的子代.

<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>
Run Code Online (Sandbox Code Playgroud)

看来我并没有真正得到这个概念.也许你可以帮助我,为什么这是不可能的,以及我如何使用ResourceDictionary旁边的一般样式.

H.B*_*.B. 16

您已经在"字典"旁边定义了资源,一个图像和一个画笔.

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

        <!-- Other resources here -->
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)


小智 5

只需在资源字典中包含 {x:type} 样式

  <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>  
Run Code Online (Sandbox Code Playgroud)