元素属性继承&;重复使用XAML更好的实践
是否有方法定义可添加到项目的属性组合 因此,不是:元素属性继承&;重复使用XAML更好的实践,xaml,Xaml,是否有方法定义可添加到项目的属性组合 因此,不是: <TextBlock Text="Hi" Foreground="Gray" Margin="0,8,0,0" FontFamily="Segoe UI"/> <TextBlock Text="there" Foreground="Gray" Margin="0,8,0,0" FontFamily="Segoe UI"/> 我会做类似的事情 <PropertyCombo name="textBlockComb
<TextBlock Text="Hi" Foreground="Gray" Margin="0,8,0,0" FontFamily="Segoe UI"/>
<TextBlock Text="there" Foreground="Gray" Margin="0,8,0,0" FontFamily="Segoe UI"/>
我会做类似的事情
<PropertyCombo name="textBlockCombo1"
Foreground="Gray" Margin="0,8,0,0" FontFamily="Segoe UI"
.
.
.
<TextBlock Text="Hi" ImportProperty="textBlockCombo1"/>
<TextBlock Text="there" ImportProperty="textBlockCombo1"/>
Ya,这真的让维护和可读性变得非常容易
只需在父实例级别执行类似的操作,或者在树的更高层执行类似于DOM的继承操作
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="30"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Margin" Value="5,10"/>
<Setter Property="Text" Value="Blah"/>
</Style>
</StackPanel.Resources/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
</StackPanel>
您还可以创建x:Key静态样式,并将它们放入资源字典中,然后在需要的地方调用它们,就像在实例中一样
<Style TargetType="TextBlock" x:Key="AwesomeStyleName">
<Setter Property="FontSize" Value="30"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Margin" Value="5,10"/>
<Setter Property="Text" Value="Blah"/>
</Style>
<TextBlock Style="{StaticResource AwesomeStyleName}"/>
然后在实例中调用
<Style TargetType="TextBlock" x:Key="AwesomeStyleName">
<Setter Property="FontSize" Value="30"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Margin" Value="5,10"/>
<Setter Property="Text" Value="Blah"/>
</Style>
<TextBlock Style="{StaticResource AwesomeStyleName}"/>
或将其应用于实例中的组,如:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource AwesomeStyleName}"/>
</StackPanel.Resources/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
</StackPanel>
希望这有帮助,干杯。任何时候,祝你周末愉快!