2012-01-06 81 views
1

我創建了一個屏幕(Screen1.xaml),它具有幾個文本框和下拉列表。由於所有的文本框的屬性是一樣的,我已創建了像寬度,高度,字體大小等這樣動態更改控件的樣式

<Style x:Key="TextBox.Base" TargetType="Control" BasedOn="{StaticResource TextBlock.Base}"> 
<Setter Property="Width" Value="250"/> 
<Setter Property="FontSize" Value="10"/> 
<Setter Property="Height" Value="15"/> 
<Setter Property="FontFamily" Value="Arial"/> 
</Style> 

現在的性能風格文件(stylesheet.xaml),我要動態變化控件的屬性取決於某些條件。我希望在代碼隱藏方面做到這一點。請幫忙。

+0

是否應用新的Style資源很重要,還是VisualStateManager會給您提供您所需的資源? – terphi 2012-01-06 17:29:14

回答

0

您可以選擇一種風格並在代碼隱藏方式中進行更改。根據您它納入到項目的方式有幾種方式:

// the style defined in the app.xaml (you need a key) 
Style globalStyle = Application.Current.Resources["Key"] as Style; 

// the style defined for a control (you need its key) 
UserControl control = ... 
Style controlStyle = control.Resources["Key"] as Style; 

// the current style of the control 
Style currentStyle = control.Style; 

改變風格是這樣的:

style.SetValue(UserControl.FontSizeProperty, (float)10); 

編輯: 正如我現在看,要改變風格只會影響在WPF中使用它的所有控件。在Silverlight中,您可能會更改所有控件的屬性:(