【问题标题】:C# multiple contentpresenter on a button style (windows 8.1)按钮样式上的 C# 多个 contentpresenter (windows 8.1)
【发布时间】:2014-06-04 12:29:17
【问题描述】:

我正在尝试使用自定义样式在按钮上显示 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}"/>

然后,这个按钮看起来像这样

Original button

无论如何,我想在样式中再添加一个“ContentPresenter”,并在按钮上再显示一个不同的数据。

但如果我在样式中再添加一个“ContentPresenter”,我不知道如何在每个“ContentPresenter”上分配 2 个不同的数据。

这是我想做的按钮。

The button I want to make

提前致谢

【问题讨论】:

    标签: c# xaml microsoft-metro windows-8.1 contentpresenter


    【解决方案1】:

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

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

    Button:

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

    【讨论】:

    • 非常感谢!这正是我想要的!! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2018-04-14
    相关资源
    最近更新 更多