【发布时间】:2019-01-27 15:36:30
【问题描述】:
我创建了一个样式,它创建一个标签为一个圆圈,中间有文本。
<Style x:Key="RoundedLabelStyle" TargetType="{x:Type Label}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Grid Height="Auto" Width="Auto" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
<Ellipse x:Name="cp" Margin="0,0,0,0" Fill="{TemplateBinding Background}" Height="{TemplateBinding Width}" Width="{TemplateBinding Width}" Stroke="Black" StrokeThickness="2" />
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center">
<ContentPresenter.Content>
<Border Padding="10">
<ContentPresenter Content="{TemplateBinding Content}"/>
</Border>
</ContentPresenter.Content>
</ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
它是这样使用的:
<Label Style="{StaticResource RoundedButtonStyle}" Content="{Binding CountValue}" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Red" BorderBrush="Red" BorderThickness="3" Height="100" Width="100" FontSize="20" FontWeight="Bold" />
这很好用。
但是,我想通过在不同位置设置两个文本字段来向此标签添加更多信息。
第一个已经存在并显示在椭圆的中心。
我想添加一个显示在椭圆下方的。
如果可能的话,我希望能够在纯 xaml 中实现它,并像这样使用它,其中与 SecondLabelText 的绑定显示在 Ellipse 下:
<Label Style="{StaticResource RoundedButtonStyle}" Content="{Binding CountValue}" SecondContent="{Binding SecondLabelText}" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Red" BorderBrush="Red" BorderThickness="3" Height="100" Width="100" FontSize="20" FontWeight="Bold" />
我可以将标签添加到样式中,但是如何设置两个单独的内容?
【问题讨论】:
-
一个标签元素只有一个内容。如果您需要多个内容,请创建自定义控件。作为“次要内容”的解决方法,人们经常使用 Tag 属性。
-
@Clemens Tag 是个好主意。谢谢。
-
只需创建您自己的用户控件。关注this 文章了解技巧。您可以根据需要绑定任意数量的属性