如何使用 ComboBox 更改主题 XAML? [英] How to Change Theme XAML with ComboBox?

查看:30
本文介绍了如何使用 ComboBox 更改主题 XAML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I apply different color themes to my program using a ComboBox?

ThemeBlue.xamlThemeRed.xamlThemePurple.xaml.

我需要它在选择更改时将一个主题 xaml 文件换成另一个.

I need it to swap out one theme xaml file for another on selection change.

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

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

I have a theme file ThemeBlue.xaml with colors set for controls and window.

<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}"/>
...

<小时>

组合框主题选择

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#

这是我在应用主题文件时需要帮助的地方.

This is where I need help to apply the theme file.

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

<小时>

应用程序.xaml

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


App.xaml

The theme file is loaded through App.xaml at startup

<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>

<小时>

主窗口.xaml

应用了主题样式的文本框:


MainWindow.xaml

A TextBox with the Theme Style applied:

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

<小时>

保存主题

我通过设置保存和加载主题.


Saving Theme

I save and load the theme through Settings.

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();
}

推荐答案

试试这个:

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>

这篇关于如何使用 ComboBox 更改主题 XAML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆