上一篇我们说了如何引用样式的资源文件,这次我们说一下如何切换文件的引用来达到切换主题。
创建样式文件
首先我们新建两个样式文件
button.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfStyleSwitch">
<Style TargetType="{x:Type Button}">
<Setter Property="Height" Value="25"></Setter>
<Setter Property="Width" Value="200"></Setter>
<Setter Property="Foreground" Value="#FF00FF23"/>
<Setter Property="Background" Value="Red"/>
</Style>
<Style x:Key="But" TargetType="{x:Type Button}">
<Setter Property="Height" Value="25"></Setter>
<Setter Property="Width" Value="200"></Setter>
<Setter Property="Foreground" Value="#FF00FF23"/>
<Setter Property="Background" Value="Blue"/>
</Style>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Background" Value="Red"/>
</Style>
<Style x:Key="Gbg" TargetType="{x:Type Grid}">
<Setter Property="Background" Value="Aqua"/>
</Style>
</ResourceDictionary>fancyButton.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfStyleSwitch">
<Style TargetType="{x:Type Button}">
<Setter Property="Height" Value="25"></Setter>
<Setter Property="Width" Value="200"></Setter>
<Setter Property="Foreground" Value="#FF00FF23"/>
<Setter Property="Background" Value="#FFBFAF0C"/>
</Style>
<Style x:Key="But" TargetType="{x:Type Button}">
<Setter Property="Height" Value="25"></Setter>
<Setter Property="Width" Value="200"></Setter>
<Setter Property="Foreground" Value="#FF00FF23"/>
<Setter Property="Background" Value="BlueViolet"/>
</Style>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Background" Value="#FFBFAF0C"/>
</Style>
<Style x:Key="Gbg" TargetType="{x:Type Grid}">
<Setter Property="Background" Value="#FF00FF23"/>
</Style>
</ResourceDictionary>
引用默认资源
<Application x:Class="WpfStyleSwitch.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfStyleSwitch"
StartupUri="MainWindow.xaml">
<Application.Resources>
<!--引用外部资源-->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/button.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
主页面使用
<Window x:Class="WpfStyleSwitch.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfStyleSwitch"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu Grid.Row="0" VerticalAlignment="Top">
<MenuItem Header="皮肤" >
<MenuItem Header="简单按钮风格" Name="simpleSkin" Click="MenuItem_Click" IsChecked="True"></MenuItem>
<MenuItem Header="个性化按钮风格" Name="fancySkin" Click="MenuItem_Click"></MenuItem>
</MenuItem>
</Menu>
<Button Grid.Row="1" Content="OK" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Grid Grid.Row="2" Style="{DynamicResource Gbg}">
<Button Style="{DynamicResource But}" Content="OK" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</Grid>
</Window>
后台切换
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = (MenuItem)sender;
if (menuItem == null) return;
string style = "button";
if (menuItem == simpleSkin)
{
style = "button";
menuItem.IsChecked = true;
fancySkin.IsChecked = false;
}
else if (menuItem == fancySkin)
{
style = "fancyButton";
menuItem.IsChecked = false;
fancySkin.IsChecked = true;
}
SwitchStyle(style);
}
private void SwitchStyle(string style)
{
ResourceDictionary newDictionary = new ResourceDictionary();
newDictionary.Source = new Uri(@"pack://application:,,,/WpfStyleSwitch;component/Resources/"+ style + ".xaml",
UriKind.RelativeOrAbsolute);
Application.Current.Resources.MergedDictionaries[0] = newDictionary;
//方法二
//newDictionary.Source = new Uri(@"Resources/" + style + ".xaml", UriKind.Relative);
//Resources.MergedDictionaries.Add(newDictionary);
//Application.Current.Resources.MergedDictionaries.Last().Source = newDictionary.Source;//第二种2
//App.Current.Resources.MergedDictionaries.Last().Source = newDictionary.Source;//第二种3
}备注:这里发现用第二种方法如果用key来标注样式,是改变不了样式的。例如样式文件里 x:Key="Gbg"就不生效。所以建议用第一种方式。

本文介绍了一种在WPF应用程序中实现主题切换的方法。通过创建不同的样式资源文件,并利用XAML资源字典来动态更新应用的外观。文章详细展示了如何在主窗口中设置菜单项以触发样式更改。
 WPF切换主题&spm=1001.2101.3001.5002&articleId=51578979&d=1&t=3&u=ebc4a26638654a9db4e979fe7fb0f725)
1476

被折叠的 条评论
为什么被折叠?



