2016-04-13 29 views
0

aldığım çocuklarının belirli stilleri uygulamak istediğiniz benim uygulamada StackPanels birkaç: Yerine vermenin olsa tekrar tekrar I şu 5 satırları yazmaWPF Otomatik atama stilleri

<StackPanel.Resources> 
    <Style TargetType="TextBlock" BasedOn="{StaticResource SettingLabel}" /> 
    <Style TargetType="DockPanel" BasedOn="{StaticResource SettingRow}" /> 
    <Style TargetType="CheckBox" BasedOn="{StaticResource SettingCheckBox}" /> 
    <Style TargetType="PasswordBox" BasedOn="{StaticResource DialogPasswordBox}" /> 
    <Style TargetType="TextBox" BasedOn="{StaticResource DialogTextBox}" /> 
</StackPanel.Resources> 

StackPanel'un kendisi bunları uygulayacak ve dolayısıyla artıklığı azaltacak bir stil.

<Style x:Key="SettingPanel" TargetType="StackPanel"> 
    <Setter Property="Resources"> 
     <Setter.Value> 
      <Style TargetType="TextBlock" BasedOn="{StaticResource SettingLabel}" /> 
      <Style TargetType="DockPanel" BasedOn="{StaticResource SettingRow}" /> 
      <Style TargetType="CheckBox" BasedOn="{StaticResource SettingCheckBox}" /> 
      <Style TargetType="PasswordBox" BasedOn="{StaticResource DialogPasswordBox}" /> 
      <Style TargetType="TextBox" BasedOn="{StaticResource DialogTextBox}" /> 
     </Setter.Value> 
    </Setter> 
</Style> 

Yani her çocuk stilleri ayarlamak ve tekrarlamak zorunda kalmadan bunu yapmak için başka bir yol yoktur: hiçbir bağımlılık özelliği çünkü

Bir stil setter Resources ayarlamak mümkün değildir atama stilleri?

cevap

3

StackPanel ürününün Style.Resources stillerini tanımlayabilirsiniz. Bunlar, SettingPanel'i stil olarak kullanarak StackPanel'un tüm çocuklarına uygulanacaktır.

<Style x:Key="SettingPanel" TargetType="StackPanel"> 
    <Style.Resources> 
      <Style TargetType="TextBlock" BasedOn="{StaticResource SettingLabel}" /> 
      <Style TargetType="DockPanel" BasedOn="{StaticResource SettingRow}" /> 
      <Style TargetType="CheckBox" BasedOn="{StaticResource SettingCheckBox}" /> 
      <Style TargetType="PasswordBox" BasedOn="{StaticResource DialogPasswordBox}" /> 
      <Style TargetType="TextBox" BasedOn="{StaticResource DialogTextBox}" /> 
    </Style.Resources> 
</Style> 
İlgili konular