WPF是「以資源爲中心」的。您可以在資源中定義所有用戶界面樣式,畫筆和模板,並且在運行時可以很容易地啓用應用程序範圍內的主題更改,從而涵蓋您提到的所有屬性。下面是我如何做到這一點,我MainViewModel它通過其SettingsViewModel從我的設置窗口收到消息後:
private void ApplyTheme()
{
Application.Current.Resources.MergedDictionaries.Clear();
var rd = new ResourceDictionary { { "Locator", locator } };
Application.Current.Resources.MergedDictionaries.Add(rd);
switch (theme)
{
case "Blue":
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) });
break;
case "Summer":
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) });
break;
}
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Brushes.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/ControlTemplates.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/DataTemplates.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Styles.xaml", UriKind.RelativeOrAbsolute) });
}
顯然我使用Telerik控制,所以我加載它們的字典,但在方法的底部,您會請注意我也加載了我自己的資源,例如畫筆,樣式等。
總之,使用WPF進行應用程序範圍的主題更改並不容易。
發佈相關代碼和XAML。順便說一句WPF有[內置支持主題](http://stackoverflow.com/a/11139598/643085),恐龍winforms完全沒有,這就是爲什麼你被迫在winforms各種可怕的黑客,而在WPF中,一切都美麗而快樂。 –
主題技巧是通過替換'HandledWindow'的當前實例的資源完成的,我也在想這個,但是我需要一些能夠在數據綁定的同時改變其他窗口的主題,甚至不需要加載任何XAML頁面僅通過C#代碼,爲該屬性設置一個新值,並且可視化UI應該在同一時刻更新。我會在幾分鐘後發佈我的代碼。 請注意,這些靜態公共屬性也應該可以通過用戶界面進行編輯,這意味着應該有另一個數據綁定從UI進行設置。 – Lispwave
否。如果您希望應用程序範圍的主題加載「System.Windows.Application.Current.Resources」中的資源字典。 –