【问题标题】:Change wpf style while program running在程序运行时更改 wpf 样式
【发布时间】:2018-10-09 07:16:34
【问题描述】:

这就是我在我的app.xaml 中输入的内容,我在代码的很多地方都使用了这种风格:

<Style x:Key="windowStyleDefault">
    <Setter Property="Control.Background" Value="#F0F0F0" />
    <Setter Property="Control.Foreground" Value="#179DD1" />
</Style>

我想改变整个应用程序的字体和颜色(让我们从颜色开始):

<Menu  Style="{DynamicResource windowStyleDefault}" >
    <MenuItem Header="File" >
        <MenuItem x:Name="NewFarmReport" Header="New Farm Report" Click="NewFarmReport_Click"/>
        <Separator/>
        <MenuItem x:Name="Exit" Header="Exit" Click="Exit_Click"/>
    </MenuItem>
    <MenuItem Header="Settings">
        <MenuItem x:Name="GuiSettings" Header="GUI Settings" Click="GuiSettings_Click"/>
        <MenuItem x:Name="CurrentWeightSettings" Header="Current Weights Settings" Click="CurrentWeightSettings_Click"/>
        <MenuItem x:Name="DefaultWeightSettings" Header="Default Weights Settings" Click="DefaultWeightSettings_Click"/>
    </MenuItem>
    <MenuItem Header="View">
        <MenuItem Header="Show History" x:Name="ShowHistory" Click="ShowHistory_Click"/>
    </MenuItem>
    <MenuItem Header="Compare" x:Name="CompateBtn" Click="CompateBtn_Click">
    </MenuItem>
</Menu>

在这段代码中,我使用 windowStyleDefault 声明了一个菜单:

<Grid DockPanel.Dock="Top" Style="{StaticResource windowStyleDefault}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="6*"/>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>    
    <Label FontStretch="Normal" FontWeight="Bold" FontSize="17" DockPanel.Dock="Top" Content="Report History" Style="{StaticResource windowStyleDefault }" Margin="0,0,2,2" />    
    <Image x:Name="OpenSlectionMode" Margin="0,0,6,-0.4" MouseEnter="OpenSlectionMode_MouseEnter" MouseLeave="OpenSlectionMode_MouseLeave"
                           MouseLeftButtonDown="OpenSlectionMode_MouseLeftButtonDown" MaxWidth="30" MaxHeight="30" Grid.ColumnSpan="2" HorizontalAlignment="Right" Width="16"
                           Source="{StaticResource selection}"/>    
    <Image x:Name="ClosdeHistoryImage" Margin="0,0,6,-0.4" MouseLeftButtonDown="CloseHistoryImage_MouseLeftButtonDown" 
                           MouseEnter="CloseHistoryImage_MouseEnter" MouseLeave="CloseHistoryImage_MouseLeave"
                           MaxWidth="20" MaxHeight="20" Grid.ColumnSpan="3" HorizontalAlignment="Right" Width="16"
                           Source="{StaticResource CloseHistoryNormal}"/>    
</Grid>

我在这里使用DockPanel

【问题讨论】:

    标签: c# wpf xaml styles app.xaml


    【解决方案1】:

    你似乎已经把它的一部分覆盖在那里。
    如果你使用动态资源:

    <Menu  Style="{DynamicResource windowStyleDefault}" >
    

    然后它将获取 windowStyleDefault 的更改。 它必须是整个被替换的东西。
    您至少可以通过两种方式做到这一点。
    您可以在另一个资源字典中合并到 application.current.resources 或包含您希望应用更改的范围内的控件资源。如果该资源字典的条目具有相同的键和不同的内容,那么它会改变。
    或者你可以在代码中做到这一点。

    从任何你喜欢的地方获取一个样式然后设置它:

    Application.Current.Resources["windowStyleDefault"] = yourNewStyle;
    

    如果您改为使用 staticresource,则不会进行更改。

    这些是否适合你,我只能猜测。

    【讨论】:

      【解决方案2】:

      我知道一种方法,它的ResourceDictionary,在许多情况下非常简单和有用,例如使您的应用程序多语言或添加主题..

      首先,对于您要添加的每个主题,您应该将 ResourceDictionary 添加到您的项目中,并在该特定 ResourceDictionary 中定义样式或字体或画笔或您要用作主题的任何资源。

      示例(MyFirstTheme.xaml):

      <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      
          <FontFamily x:Key="MyFont">Segoe UI</FontFamily>
          <SolidColorBrush x:Key="MyThemeColor" Color="#FF34495E"/>
      
      </ResourceDictionary>
      

      示例(MySecondTheme.xaml):

      <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      
          <FontFamily x:Key="MyFont">Tahoma</FontFamily>
          <SolidColorBrush x:Key="MyThemeColor" Color="#D89A9E"/>
      
      </ResourceDictionary>
      

      您应该添加尽可能多的主题资源字典。要将其设置为默认值,您应该在 App.xaml 文件中这样处理它:

      <Application x:Class="MyApp.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>
                  <ResourceDictionary.MergedDictionaries>
      
                      <ResourceDictionary Source="Themes\MyFirstTheme.xaml"/> 
                      // This will be the default theme
      
                  </ResourceDictionary.MergedDictionaries>
              </ResourceDictionary>
          </Application.Resources>
      </Application>
      

      要为对象或控件分配主题,您应该执行以下操作:

      <Window x:Name="window" x:Class="MyApp.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"
              Title="MyApp" Height="400" Width="400"  FontFamily="{DynamicResource MyFont}">
      
              <Grid>
                      <TextBlock Foreground="{DynamicResource MyThemeColor}" Text="Theme Test" />
              </Grid>
      </Window>
      

      注意:当您在控件或对象上使用资源时,绑定必须定义为 DynamicResource

      假设您已经添加了所有主题词典,并且您想在后面的代码中切换它们,请使用此函数:

      public void ChangeTheme(string ThemeName)
      {
           ResourceDictionary dict = new ResourceDictionary();
                      dict.Source = new Uri("..\\Themes\\" + ThemeName + ".xaml", UriKind.Relative);
                      App.Current.Resources.MergedDictionaries.Add(dict);
      }
      

      用法:

       ChangeTheme("MySecondTheme");
       UpdateLayout();
      

      【讨论】:

      • 这差不多花了我1个小时才给你写下来,如果是你需要的答案,请标记为答案,谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 2020-05-08
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多