簡體   English   中英

如何使用MahApps.Metro在所有WPF窗口中應用相同的主題

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM