我正在尝试使用自定义样式在按钮上显示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个不同的数据。'
这是我想要制作的按钮。
提前致谢
答案 0 :(得分: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>