程序运行时更改wpf样式

时间:2018-04-28 17:44:58

标签: c# wpf xaml styles app.xaml

这就是我在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

2 个答案:

答案 0 :(得分:0)

你好像有一部分在那里 如果您使用dynamicresource:

<Menu  Style="{DynamicResource windowStyleDefault}" >

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

从任何地方获取样式,然后进行设置:

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

如果您改为使用staticresource,那么它将无法获取更改。

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

答案 1 :(得分:0)

我知道有一种方法,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();