c#多个内容呈现器的按钮样式(windows 8.1)



我想在一个按钮上显示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个不同的数据。

这就是我想做的按钮。

我要创建的按钮

Thanks in advance

您可以使用按钮上的Tag属性来分配额外的数据并绑定第二个ContentPresenter Content,如下所示:

   <ContentPresenter 
       Grid.Row="1"
       Content="{TemplateBinding Tag}"
       HorizontalAlignment="Center" 
       VerticalAlignment="Center">
   </ContentPresenter>

和on Button:

 <Button Tag="{Binding OTHERDATAPROPERTY}" Content="{Binding Comments}"></Button>

最新更新