C# 动态更改基础样式

C# 动态更改基础样式,c#,wpf,xaml,C#,Wpf,Xaml,我试图超越基本风格的属性,但在运行时。 例如,我有一个设置页面,允许用户更改字体大小和字体系列等。所有这些都是通用属性。所以,我有一个结构,我定义了所有这些基本属性。现在,当我将字体大小从11px更改为14px时,应用程序中的所有元素都应该继承此更改 问题是我无法修改存储所有属性的基本样式 下面的代码显示了我的基本样式: <Style x:Key="BaseStyle"> <Setter Property="Control.FontFamily" Value="

我试图超越基本风格的属性,但在运行时。 例如,我有一个设置页面,允许用户更改字体大小和字体系列等。所有这些都是通用属性。所以,我有一个结构,我定义了所有这些基本属性。现在,当我将字体大小从11px更改为14px时,应用程序中的所有元素都应该继承此更改

问题是我无法修改存储所有属性的基本样式

下面的代码显示了我的基本样式:

<Style x:Key="BaseStyle">
        <Setter Property="Control.FontFamily" Value="Arial"></Setter>
        <Setter Property="Control.FontSize" Value="11px"/>
        <Setter Property="Control.Foreground" Value="Red"/>
</Style>
 <Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}">
       <Setter Property="Control.Background" Value="{DynamicResource NormalBrush}"/>
 </Style>

现在,我有了另一个继承自此基础样式的样式:

<Style x:Key="BaseStyle">
        <Setter Property="Control.FontFamily" Value="Arial"></Setter>
        <Setter Property="Control.FontSize" Value="11px"/>
        <Setter Property="Control.Foreground" Value="Red"/>
</Style>
 <Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}">
       <Setter Property="Control.Background" Value="{DynamicResource NormalBrush}"/>
 </Style>

在应用程序中,我有一个用于更改字体大小的组合框:

<ComboBox Height="23" HorizontalAlignment="Left" Name="comboBox2" SelectedValue="FontSizeValue" Style="{x:Null}" Width="92">
                        <ComboBoxItem Content="12px"/>
                        <ComboBoxItem Content="13px"/>
                        <ComboBoxItem Content="14px"/>
                        <ComboBoxItem Content="15px"/>
</ComboBox>


现在,当我在应用程序中从这个组合框中选择一个值时,我必须更新基本样式。这是我无法做到的。关于如何实现这一目标的任何建议。所有属性更改都应该是动态发生的。

基本样式应该是这种类型的控件不会更改的值。需要更改的值以单独的样式指定,该样式可以继承基础。例如:

<Window.Resources>
    <!-- Main style for all controls -->
    <Style x:Key="BaseStyle" TargetType="{x:Type Control}">
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="FontSize" Value="11px" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="Width" Value="200" />
        <Setter Property="Height" Value="25" />
    </Style>

    <!-- This style inherits all the settings from the base style, but set the background -->
    <Style x:Key="DefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="Green" />
    </Style>

    <!-- This style inherits only the width and height -->
    <Style x:Key="NotDefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="Black" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontFamily" Value="Courier New" />
    </Style>
</Window.Resources>

<Grid>
    <StackPanel>
        <TextBox Style="{StaticResource DefaultBaseStyle}" Text="Default base style" Margin="0,10,0,0" />
        <TextBox Style="{StaticResource NotDefaultBaseStyle}" Text="Not default base style" Margin="0,10,0,0" />
    </StackPanel>
</Grid>
现在setter引用设置中的值。更改代码后面的属性:

// your namespace.Properties.Settings.Default.your name of property
DynamicStyleHelp.Properties.Settings.Default.MyColor = "Red";   
下面是一个完整的示例:

XAML

<Window x:Class="DynamicStyleHelp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:properties="clr-namespace:DynamicStyleHelp.Properties"
    Title="MainWindow" Height="350" Width="525"
    WindowStartupLocation="CenterScreen">

<Window.Resources>
    <Style x:Key="BaseStyle" TargetType="{x:Type Control}">
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="FontSize" Value="11px" />
        <Setter Property="Background" Value="{Binding Source={x:Static properties:Settings.Default}, Path=MyColor, Mode=TwoWay}" />
        <Setter Property="Width" Value="200" />
        <Setter Property="Height" Value="25" />
    </Style>

    <Style x:Key="DefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Foreground" Value="Black" />
    </Style>

    <Style x:Key="NotDefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontFamily" Value="Courier New" />
    </Style>
</Window.Resources>

<Grid>
    <StackPanel>
        <TextBox Style="{StaticResource DefaultBaseStyle}" Text="Default base style" Margin="0,10,0,0" />
        <TextBox Style="{StaticResource NotDefaultBaseStyle}" Text="Not default base style" Margin="0,10,0,0" />

        <Button Name="ChangeButton" Width="100" Height="30" Content="ChangeButton" Margin="0,10,0,0" Click="ChangeButton_Click" />
    </StackPanel>
</Grid>
</Window>

基本样式应为此类控件不会更改的值。需要更改的值以单独的样式指定,该样式可以继承基础。例如:

<Window.Resources>
    <!-- Main style for all controls -->
    <Style x:Key="BaseStyle" TargetType="{x:Type Control}">
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="FontSize" Value="11px" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="Width" Value="200" />
        <Setter Property="Height" Value="25" />
    </Style>

    <!-- This style inherits all the settings from the base style, but set the background -->
    <Style x:Key="DefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="Green" />
    </Style>

    <!-- This style inherits only the width and height -->
    <Style x:Key="NotDefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="Black" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontFamily" Value="Courier New" />
    </Style>
</Window.Resources>

<Grid>
    <StackPanel>
        <TextBox Style="{StaticResource DefaultBaseStyle}" Text="Default base style" Margin="0,10,0,0" />
        <TextBox Style="{StaticResource NotDefaultBaseStyle}" Text="Not default base style" Margin="0,10,0,0" />
    </StackPanel>
</Grid>
现在setter引用设置中的值。更改代码后面的属性:

// your namespace.Properties.Settings.Default.your name of property
DynamicStyleHelp.Properties.Settings.Default.MyColor = "Red";   
下面是一个完整的示例:

XAML

<Window x:Class="DynamicStyleHelp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:properties="clr-namespace:DynamicStyleHelp.Properties"
    Title="MainWindow" Height="350" Width="525"
    WindowStartupLocation="CenterScreen">

<Window.Resources>
    <Style x:Key="BaseStyle" TargetType="{x:Type Control}">
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="FontSize" Value="11px" />
        <Setter Property="Background" Value="{Binding Source={x:Static properties:Settings.Default}, Path=MyColor, Mode=TwoWay}" />
        <Setter Property="Width" Value="200" />
        <Setter Property="Height" Value="25" />
    </Style>

    <Style x:Key="DefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Foreground" Value="Black" />
    </Style>

    <Style x:Key="NotDefaultBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="{x:Type TextBox}">
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontFamily" Value="Courier New" />
    </Style>
</Window.Resources>

<Grid>
    <StackPanel>
        <TextBox Style="{StaticResource DefaultBaseStyle}" Text="Default base style" Margin="0,10,0,0" />
        <TextBox Style="{StaticResource NotDefaultBaseStyle}" Text="Not default base style" Margin="0,10,0,0" />

        <Button Name="ChangeButton" Width="100" Height="30" Content="ChangeButton" Margin="0,10,0,0" Click="ChangeButton_Click" />
    </StackPanel>
</Grid>
</Window>

MSDN表示:一旦应用了样式,它将被密封,无法更改。如果要动态更改已应用的样式,必须创建新样式以替换现有样式。是。这就是我现在面临的问题。我们不能找一份同样的工作吗?我的意思是,如果我只更改字体大小的属性,而其他10个属性将保持不变,那么创建一个新样式并更新它将覆盖当前样式,我将丢失所有默认值?那么还有其他的解决方法吗?MSDN说:一旦应用了一种样式,它就被密封起来,无法更改。如果要动态更改已应用的样式,必须创建新样式以替换现有样式。是。这就是我现在面临的问题。我们不能找一份同样的工作吗?我的意思是,如果我只更改字体大小的属性,而其他10个属性将保持不变,那么创建一个新样式并更新它将覆盖当前样式,我将丢失所有默认值?还有其他的解决方法吗?我想通过点击按钮或UI上的某个事件来实现这一点。可能吗?我看到了一些可以在setter值上进行的绑定。但我不明白如何使用它们。@Deeksha:你能详细写下你需要的吗。我已经编辑了我的问题。请查收。我希望它更清楚一点。我的问题是实现动态更新样式,包括更改基础样式本身。所以我需要一个方法来绑定setter属性的值。但我不知道如何使用它,我想通过点击按钮或UI上的某个事件来实现这一点。可能吗?我看到了一些可以在setter值上进行的绑定。但我不明白如何使用它们。@Deeksha:你能详细写下你需要的吗。我已经编辑了我的问题。请查收。我希望它更清楚一点。我的问题是实现动态更新样式,包括更改基础样式本身。所以我需要一个方法来绑定setter属性的值。但我不知道如何使用它。