【发布时间】:2017-04-09 12:55:29
【问题描述】:
如何使用 ComboBox 将不同的颜色主题应用于我的程序?
如ThemeBlue.xaml、ThemeRed.xaml、ThemePurple.xaml。
我需要它在选择更改时将一个主题 xaml 文件换成另一个。
这是示例项目文件:
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}"/>
...
组合框主题选择
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 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();
}
【问题讨论】:
标签: c# wpf visual-studio xaml