C# 按钮样式上的多个 contentpresenter (windows 8.1)
-
21-12-2019 - |
题
我正在尝试使用自定义样式在按钮上显示 2 个数据。
因此,我编写了如下所示的 XAML 代码,其中包含一个“ContentPresenter”。
<Style x:Key="Num_of_Comments" TargetType="Button">
<Setter Property="Foreground" Value="{ThemeResource AppBarItemForegroundThemeBrush}"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="Transparent" x:Name="RootGrid">
<Grid
Height="42"
Width="42">
<Ellipse
x:Name="Ellipse"
Fill="{ThemeResource AppBarItemBackgroundThemeBrush}"
Stroke="{TemplateBinding Foreground}"
StrokeThickness="2"
UseLayoutRounding="False" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="21"/>
<RowDefinition Height="21"/>
</Grid.RowDefinitions>
<ContentPresenter
Grid.Row="0"
Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="Center"
VerticalAlignment="Center">
</ContentPresenter>
</Grid>
</Grid>
<Rectangle x:Name="FocusVisualWhite" IsHitTestVisible="False" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{ThemeResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1"/>
<Rectangle x:Name="FocusVisualBlack" IsHitTestVisible="False" Opacity="0" StrokeDashOffset="0.5" StrokeEndLineCap="Square" Stroke="{ThemeResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我就是这样使用这种风格的。
<Button Grid.Column="1" Content="{Binding Comments}" Style="{StaticResource Num_of_Comments}" IsEnabled="False" Margin="10,0,0,0" VerticalAlignment="Center" Foreground="{Binding Comments_color}"/>
然后,这个按钮看起来像这样
不管怎样,我想在样式中再添加一个“ContentPresenter”,并在按钮上再显示一个不同的数据。
但是,如果我在样式中再添加一个“ContentPresenter”,我不知道如何在每个“ContentPresenter”上分配 2 个不同的数据。
这就是我想要制作的按钮。
提前致谢
解决方案
您可以使用 Tag
按钮上的属性用于分配附加数据并绑定第二个 ContentPresenter
Content
如下:
<ContentPresenter
Grid.Row="1"
Content="{TemplateBinding Tag}"
HorizontalAlignment="Center"
VerticalAlignment="Center">
</ContentPresenter>
等等 Button
:
<Button Tag="{Binding OTHERDATAPROPERTY}" Content="{Binding Comments}"></Button>
不隶属于 StackOverflow