C# WPF 切换主题的几种方法
By
jerryxjr1220
at 2023-11-02 • 0人收藏 • 349人看过
<hc:GlowWindow x:Class="WpfThemeDemo.Views.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:model="clr-namespace:WpfThemeDemo.Models" xmlns:vm="clr-namespace:WpfThemeDemo.ViewModels" xmlns:view="clr-namespace:WpfThemeDemo.Views" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:local="clr-namespace:WpfThemeDemo" xmlns:hc="https://handyorg.github.io/handycontrol" d:DataContext="{d:DesignInstance Type=vm:MainViewModel}" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" FontFamily="JetBrains Mono" FontSize="{StaticResource TextFontSize}"> <hc:GlowWindow.Resources> <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> <Style x:Key="TextBlockTheme" TargetType="{x:Type TextBlock}"> <Style.Triggers > <DataTrigger Binding="{Binding ElementName=tbTheme, Path=IsChecked}" Value="True"> <Setter Property="Foreground" Value="LightBlue" /> </DataTrigger> <DataTrigger Binding="{Binding ElementName=tbTheme, Path=IsChecked}" Value="False"> <Setter Property="Foreground" Value="DarkBlue" /> </DataTrigger> </Style.Triggers> </Style> <Style x:Key="TextBoxTheme" TargetType="{x:Type TextBox}"> <Style.Triggers > <DataTrigger Binding="{Binding ElementName=tbTheme, Path=IsChecked}" Value="True"> <Setter Property="Foreground" Value="Blue" /> <Setter Property="Background" Value="White" /> </DataTrigger> <DataTrigger Binding="{Binding ElementName=tbTheme, Path=IsChecked}" Value="False"> <Setter Property="Foreground" Value="White" /> <Setter Property="Background" Value="Gray" /> </DataTrigger> </Style.Triggers> </Style> <Style x:Key="ButtonTheme" TargetType="{x:Type Button}"> <Style.Triggers > <DataTrigger Binding="{Binding ElementName=tbTheme, Path=IsChecked}" Value="True"> <Setter Property="Foreground" Value="{DynamicResource InfoBrush}" /> <Setter Property="Background" Value="White"/> </DataTrigger> <DataTrigger Binding="{Binding ElementName=tbTheme, Path=IsChecked}" Value="False"> <Setter Property="Foreground" Value="White" /> <Setter Property="Background" Value="DarkBlue"/> </DataTrigger> </Style.Triggers> </Style> <Style x:Key="ToggleButtonTheme" TargetType="{x:Type ToggleButton}"> <Style.Triggers > <DataTrigger Binding="{Binding ElementName=tbTheme, Path=IsChecked}" Value="True"> <Setter Property="Foreground" Value="{DynamicResource InfoBrush}"/> </DataTrigger> <DataTrigger Binding="{Binding ElementName=tbTheme, Path=IsChecked}" Value="False"> <Setter Property="Foreground" Value="DarkBlue"/> </DataTrigger> </Style.Triggers> </Style> </hc:GlowWindow.Resources> <Grid> <StackPanel Grid.Column="0"> <TextBlock Text="Light Theme" FontSize="20" Margin="20" Style="{DynamicResource TextBlockTheme}" /> <TextBox Width="400" FontSize="20" Margin="20" Style="{DynamicResource TextBoxTheme}"/> <Button Content="Button" FontSize="20" Height="45" HorizontalAlignment="Center" Style="{DynamicResource ButtonTheme}"/> </StackPanel> <ToggleButton Grid.Column="0" Content="Theme" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="20" Style="{StaticResource ToggleButtonTheme}" x:Name="tbTheme" IsChecked="True" Width="100" Click="TbTheme_OnClick"/> </Grid> </hc:GlowWindow>
using CommunityToolkit.Mvvm.Messaging; using CommunityToolkit.Mvvm.Messaging.Messages; using HandyControl.Controls; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using WpfThemeDemo.Models; using WpfThemeDemo.ViewModels; using MessageBox = HandyControl.Controls.MessageBox; namespace WpfThemeDemo.Views { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : GlowWindow { private MainViewModel mainViewModel; public MainWindow() { InitializeComponent(); #region 注册并接受消息 //WeakReferenceMessenger.Default.Register<CustomizedMessage>(this, (o, m) => { MessageBox.Show("Received!"); }); #endregion mainViewModel = new MainViewModel(); this.DataContext = mainViewModel; } private void TbTheme_OnClick(object sender, RoutedEventArgs e) { if (tbTheme.IsChecked == true) this.Background = Brushes.White; else this.Background = Brushes.Gray; } } }
2 个回复 | 最后更新于 2023-11-03
2023-11-03
#2
登录后方可回帖
用资源字典好点吧,替换字典整个都改变