首先,我想以尽可能少的开销为我的GUI添加自定义文本块。例如:<TextBlock style={StaticRessources myTextBlock}>Text</TextBlock>
现在我有以下边框样式:
<Style x:Key="greenBox" TargetType="Border">
<Setter Property="Background" Value="#00FF00"/>
<Setter Property="CornerRadius" Value="10"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Width" Value="100"/>
</Style>
我按照以下方式应用它:
<Border Style="{StaticResource greenBox}">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Custom Text</TextBlock>
</Border>
我的问题是,它需要2个标签,并且TextBlock中设置的属性将是冗余的。我无法弄清楚如何将两个定义抽象为单个元素。
答案 0 :(得分:3)
这就是Label发挥作用的地方:
<Style TargetType="Label" x:Key="greenLabel">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border Style="{StaticResource greenBox}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Label Style="{StaticResource greenLabel}">Custom Text</Label>
(根据您的另一个问题:如果这是您使用该bordertyle的唯一地方,您当然可以直接将这些包含在该边框中,而不是使用额外的样式)
答案 1 :(得分:0)
您需要按照here所述创建自定义控件。或者您也可以创建UserControl。