2013-08-19 107 views
1

我試圖在運行時超過基本樣式屬性。 例如我有一個設置頁面,我允許用戶更改字體大小和字體系列等。所有這些都是常用屬性。所以,我有一個定義所有這些基本屬性的結構。現在,當我將字體大小從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> 

而且在我對字體大小變化的組合框的應用:

<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> 

現在,當我從應用程序中的組合框中選擇一個值時,我將不得不更新基礎樣式。我無法做到這一點。關於如何實現這一點的任何建議。所有的財產變化都應該動態發生。

+2

MSDN說:一旦風格得到了應用,它是密封並且不能改變。如果要動態更改已應用的樣式,則必須創建新樣式來替換現有樣式。 – Nitesh

+0

是的。那就是我現在面臨的問題。難道我們可以得到相同的工作嗎?我的意思是,如果我只更改字體大小的屬性和其他10個屬性將保持原樣,那麼創建新樣式並更新它將覆蓋當前樣式,並且會丟失所有默認值rite?那麼還有其他解決方法嗎? – ds345

回答

1

基本風格應該是這種類型的控制不會改變的值。需要更改的值以單獨的樣式指定,該樣式可以繼承基礎。例如:

<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

enter image description here

如果你有很多不同類型的控件,那麼也許是更好地共同選擇的東西創造爲他們每個人的基本風格(例如:寬度,高度,對齊)。例如,基本樣式爲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"; 
    } 
} 
+0

我想通過一些按鈕點擊或UI上的某個事件來實現此目的。那可能嗎?我看到了一些可以在setter值上完成的綁定。但我無法理解如何使用它們。 – ds345

+0

@Deeksha:你能詳細寫下你需要什麼嗎?如何處理事件的風格? –

+0

我編輯了我的問題。請檢查。我希望清楚一點。我的問題是動態更新樣式,其中可能包括更改基礎樣式本身。所以我用一個方法來綁定setter屬性的值。但我無法理解如何使用它。 – ds345