如何使用ComboBox更改主题XAML?

时间:2017-04-09 12:55:30

标签: c# wpf visual-studio xaml

如何使用ComboBox将不同的颜色主题应用于我的程序?

例如ThemeBlue.xamlThemeRed.xamlThemePurple.xaml

我需要它在选择更改时换掉另一个主题xaml文件。

Blue Theme Example

这是示例项目文件:
https://drive.google.com/open?id=0BycnCTAQ1U7gSU5kUUdaNzRIZDg

主题

我有一个主题文件ThemeBlue.xaml,其颜色设置为控件和窗口。

<SolidColorBrush x:Key="TextBoxCustom.Static.Background" Color="Blue"/>
<SolidColorBrush x:Key="TextBoxCustom.Static.Border" Color="Blue"/>

<Style x:Key="TextBoxCustom" TargetType="{x:Type TextBox}">
    <Setter Property="Background" Value="{StaticResource TextBoxCustom.Static.Background}"/>
    <Setter Property="BorderBrush" Value="{StaticResource TextBoxCustom.Static.Border}"/>
...

ComboBox主题选择

XAML

<ComboBox x:Name="cboTheme" Style="{StaticResource ComboBoxCustom}" HorizontalAlignment="Left" Margin="199,120,0,0" VerticalAlignment="Top" Width="75" SelectionChanged="themeSelect_SelectionChanged">
    <System:String>Blue</System:String>
    <System:String>Red</System:String>
    <System:String>Purple</System:String>
</ComboBox>

C#

这是我需要帮助来应用主题文件的地方。

private void themeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Apply theme file to App.xaml from option selected
    // Blue
    // Red
    // Purple
}

的App.xaml

主题文件在启动时通过App.xaml加载

<Application x:Class="MyProgram.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary Source="ThemeBlue.xaml"/>
    </Application.Resources>

</Application>

MainWindow.xaml

应用了主题样式的TextBox:

<TextBox x:Name="textBox1" Style="{StaticResource TextBoxCustom}" HorizontalAlignment="Left" Height="22" Margin="93,43,0,0" Width="464" />

保存主题

我通过设置保存并加载主题。

private void themeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Save Theme for next launch
    Settings.Default["Theme"] = cboTheme.SelectedItem.ToString();
    Settings.Default.Save();
    Settings.Default.Reload();
}

2 个答案:

答案 0 :(得分:1)

试试这个:

private void themeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string theme = cboTheme.SelectedItem.ToString();

    App.Current.Resources.MergedDictionaries.Clear();
    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Theme" + theme + ".xaml", UriKind.RelativeOrAbsolute) });
    // Save Theme for next launch
    Settings.Default["Theme"] = theme;
    Settings.Default.Save();
}

<强> App.xaml中:

<Application x:Class="MyProgram.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ThemeBlue.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

答案 1 :(得分:0)

您需要将App.xaml的资源替换为后面的代码中的另一个资源。您可以通过加载组件来获取资源。这是一个可能的解决方案:

private void themeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    AssemblyName assemblyName = Assembly.GetAssembly(this.GetType()).GetName();

    ResourceDictionary dictionary = (ResourceDictionary)Application.LoadComponent(
        new Uri(assemblyName.Name + @";component/" + cboTheme.SelectedItem + ".xaml", UriKind.Relative))));

    App.Current.Resources.MergedDictionaries.Clear();
    if (dictionary != null)
    {
        App.Current.Resources.MergedDictionaries.Add(dictionary);
    }
}