我是從Web開發和WinForms到WPF,也許我還沒有得到概念。 我可以在app.xaml中爲我的應用程序定義一般樣式。例如,我爲該文件中的所有功能區控件定義了樣式。將一般的WPF樣式與ResourceDictionary混合使用
然後我嘗試了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>
看來我並沒有真正理解這個概念。也許你可以幫助我,爲什麼這是不可能的,以及我如何使用ResourceDictionary旁邊的常規樣式。
我知道{X}類型的風格,但我是如何從MSDN的說明了它,風格在ResourceDictionary中必須有一個關鍵。但是我想定義一般樣式而沒有一個鍵,所以它們將被默認啓用。 – 2012-01-13 08:35:28
@René:所以問題是如果你添加一個與合併字典中的樣式相同的TargetType的樣式,你會得到一個錯誤? – 2012-01-13 09:35:43
不是。我只是不知道資源字典只是使用沒有密鑰的TargetType的樣式。到現在... – 2012-01-13 12:27:40