基本样式应该是不会更改此类控件的值。需要更改的值在单独的样式中指定,可以继承基础。例子:
<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>
Output
如果您有许多不同类型的控件,那么最好通过选择一些共同点(例如:宽度、高度、对齐方式)为每个控件创建一个基本样式。例如,基础样式为Button
,TextBox
等。它们的控件与基础有很大不同,您应该创建一个单独的样式来继承基础。
EDIT:
如果您想根据用户的选择更改样式,则需要使用这些参数创建设置。所以,进入项目的设置:
Project -> Properties -> Parameters
MyColor
使用名称、字符串类型创建设置。要与设置的样式相关联,您需要编写以下内容:
xmlns:properties="clr-namespace:DynamicStyleHelp.Properties"
<Setter Property="Background" Value="{Binding Source={x:Static properties:Settings.Default}, Path=MyColor, Mode=TwoWay}" />
现在 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>
Code behind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ChangeButton_Click(object sender, RoutedEventArgs e)
{
DynamicStyleHelp.Properties.Settings.Default.MyColor = "Red";
}
}