基本的なスタイルは、このタイプのコントロールでは変更されない値にする必要があります。変更が必要な値は、ベースを継承できる別のスタイルで指定されます。例:
<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
name 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";
}
}