如何使用 ComboBox 更改主题 XAML?

编程入门 行业动态 更新时间:2024-10-27 05:24:11
本文介绍了如何使用 ComboBox 更改主题 XAML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如何使用 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/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/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft/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/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">

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

</Application>

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

更多推荐

[db:关键词]

本文发布于:2023-04-27 13:14:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154950.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   主题   ComboBox   XAML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!