愤怒者系列(二) WPF切换主题

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

上一篇我们说了如何引用样式的资源文件,这次我们说一下如何切换文件的引用来达到切换主题。

创建样式文件

首先我们新建两个样式文件

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"就不生效。所以建议用第一种方式。







评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值