![](/img/trans.png)
[英]Apply MahApps.Metro theme and accent to other controls or rectangles
[英]How to apply the same theme in all WPF windows using MahApps.Metro
我是WPF項目的新手,我對組件MahApps.Metro有疑問。 我在項目中創建了一個MainWindow和其他窗口,我的問題是:如何將MainWindow中的相同主題應用到項目的其他窗口? 並且,我必須為所有窗口應用ResourceDictionary,或者只存在一次這樣做嗎?
感謝大伙們!
在App.xaml
,像這樣設置ResourceDictionary
屬性:
<Application.Resources>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</Application.Resources>
請注意,為了將資源字典中的樣式應用於該類型的所有控件,該樣式不應具有鍵 。 例如,一個定義如下的樣式:
<Style TargetType="{x:Type Button}">
<Setter Property="Content" Value="This is the default button text"/>
</Style>
在ResourceDictionary
將導致項目中的所有按鈕在創建時具有默認值。
WPF中的資源基於其可用的“范圍”工作。 請查看此文章以獲取詳細說明
現在Application
(App.xaml)在Window
的更高范圍內。 因此,要在Window
共享這些MahApps控制資源,只需將ResourceDictionary
移動到Application.Resources
范圍即可。
就像是:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
現在,您不再需要在每個Window中添加這些Dictionary,因為它們是隱式可用的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.