动态更改基本样式 [英] Make changes to the base style dynamically
问题描述
我试图在运行时覆盖基本样式属性.例如我有一个设置页面,我允许用户在其中更改字体大小和字体系列等.所有这些都是通用属性.所以,我有一个结构,在其中定义了所有这些基本属性.现在,当我将字体大小从 11px 更改为 14px 时,应用程序中的所有元素都应继承此更改.
I am trying to over ride the base style property but at run time. For e.g. I have a settings page, where I allow the user to change the font size and font family etc. All these are common properties. So, I have a structure where I have defined all these basic properties. Now when I change the font size from 11px to 14px, all the elements in the application should inherit this change.
问题是我无法修改存储所有属性的基本样式.
Problem is that I can't modify the base style which stores all the properties.
下面的代码显示了我的基本样式:
Below code shows my base 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>
现在我有另一种从这个基本样式继承的样式:
Now I have another style which inherits from this base style:
<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}">
<Setter Property="Control.Background" Value="{DynamicResource NormalBrush}"/>
</Style>
在应用程序中,我有一个用于更改字体大小的组合框:
And in the application I have a combo box for font size change:
<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>
现在,当我从应用程序的这个组合框中选择一个值时,我将不得不更新基本样式.这是我无法做到的.关于我如何实现这一目标的任何建议.所有属性更改都应该动态发生.
Now when I select a value from this combo box in the application, I will have to update the base style. Which I am not being able to do. Any suggestions on how I can achieve this. All the property changes are supposed to happen dynamically.
推荐答案
基本样式应该是对于此类控件不会更改的值.需要更改的值在单独的样式中指定,该样式可以继承基数.示例:
The basic style should be the values that will not change for this type of control. The values that need to be changed are specified in a separate style, which can inherit the base. Example:
<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>
输出
如果您有很多不同类型的控件,那么最好通过选择一些共同点(例如:宽度、高度、对齐方式)为每个控件创建一个基本样式.例如Button
、TextBox
等的基础样式,而且它们的控件与基础有很大的不同,你应该创建一个单独的继承基础的样式.
If you have a lot of different types of controls, then perhaps it is better to create a basic style for each of them by selecting something in common (eg: width, height, alignment). For example, the base style for the Button
, TextBox
, etc. And they controls that greatly different from the base, you should create a separate style that inherits the base.
如果要根据用户的选择更改样式,则需要使用这些参数创建设置.所以,进入项目的设置:
If you want to base the style changed depending on the user's choice, then you need to create a settings with these parameters. So, go into the settings of the project:
项目 ->属性 ->参数
创建一个名为 MyColor
、字符串类型的设置.要与设置的样式相关联,需要编写以下内容:
Create a setting with name MyColor
, type of string. To associate with the style of the setting, you need to write the following:
xmlns:properties="clr-namespace:DynamicStyleHelp.Properties"
<Setter Property="Background" Value="{Binding Source={x:Static properties:Settings.Default}, Path=MyColor, Mode=TwoWay}" />
现在 setter 指的是设置中的值.更改代码后面的属性:
Now setter refers to the value in the settings. Change property behind code:
// your namespace.Properties.Settings.Default.your name of property
DynamicStyleHelp.Properties.Settings.Default.MyColor = "Red";
下面是一个完整的例子:
Below is a complete example:
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>
背后的代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ChangeButton_Click(object sender, RoutedEventArgs e)
{
DynamicStyleHelp.Properties.Settings.Default.MyColor = "Red";
}
}
这篇关于动态更改基本样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!