2

我现在正在研究 XAML 中的基本样式,其中包括我的标准HandledWindow类型的模板。该样式包括几个本地资源,例如颜色、字体和其他变量,以便以后在我的样式中重用。

我正在考虑用户的 UI 设置,以便他可以根据需要更改颜色和大小等内容。但是后来我发现更改本地资源不会更改样式本身,而只会更改当前HandledWindow实例,因此它不适合 UI 设置,因为应用程序中可能有更多的运行窗口。

然后我意识到我必须相对于我的HandledWindow类的模板绑定变量,这将包括所有可更改的设置作为公共和静态属性。但是后来我遇到了静态属性绑定的问题,因为我无法引发PropertyChanged仅适用于实例的事件。并且窗口不会自行更新其样式。

此外,我试图让样式立即做出反应并立即更新,而无需重新启动。

4

1 回答 1

1

WPF 是“以资源为中心的”。您可以在资源中定义所有 UI 样式、画笔和模板,并且在运行时很容易启用包含您提到的所有属性的应用程序范围的主题更改。这是我在 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 进行应用程序范围的主题更改再简单不过了。

于 2013-11-15T03:31:56.600 回答