基本風格應該是這種類型的控制不會改變的值。需要更改的值以單獨的樣式指定,該樣式可以繼承基礎。例如:
<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";
}
}
MSDN說:一旦風格得到了應用,它是密封並且不能改變。如果要動態更改已應用的樣式,則必須創建新樣式來替換現有樣式。 – Nitesh
是的。那就是我現在面臨的問題。難道我們可以得到相同的工作嗎?我的意思是,如果我只更改字體大小的屬性和其他10個屬性將保持原樣,那麼創建新樣式並更新它將覆蓋當前樣式,並且會丟失所有默認值rite?那麼還有其他解決方法嗎? – ds345