Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
元素属性继承&;重复使用XAML更好的实践_Xaml - Fatal编程技术网

元素属性继承&;重复使用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>


希望这有帮助,干杯。

任何时候,祝你周末愉快!