0

我为我的按钮创建了一个自定义样式,如下所示:

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="CustomButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="#464646" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="BorderThickness" Value="2" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ButtonNumber">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ButtonCharacters">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ButtonNumber">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ButtonCharacters">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="0">
                            <Canvas>
                                <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
                                <TextBlock Margin="23,14,0,0" FontFamily="Segoe WP SemiLight" FontSize="48" Foreground="{StaticResource PhoneForegroundBrush}" x:Name="ButtonNumber" Text="XXX" />
                                <TextBlock Margin="54,36,0,0" FontFamily="Segoe WP Light" FontSize="18" Foreground="{StaticResource PhoneForegroundBrush}" x:Name="ButtonCharacters" Text="YYY" />
                            </Canvas>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

如您所见,我在按钮 (ButtonNumberButtonCharacters) 内添加了两个新的 TextBlock。现在我想为Text这两个 TextBlocks(标有 XXX 和 YYY)的属性使用自定义值,使其看起来像这样:

<Button Style="{StaticResource CustomButtonStyle}" ButtonNumber.Text="123" ButtonCharacters.Text="SomeText" />

我没有线索!

4

1 回答 1

2

更好的做法是继承 Button 类,添加两个依赖属性(例如 Text1 和 Text2)并添加到该类(暂时称它为 TextButton)。

然后,为 TextButton 和 ControlTemplate 设置通用模板(在 /themes/generic.xaml 中),您可以使用以下标记

      <TextBlock x:Name="ButtonNumber" Text="{TemplateBinding Text1}" />
      <TextBlock x:Name="ButtonCharacters" Text="{TemplateBinding Text2}" />

那将是最简单和最佳实践的方式。

于 2012-07-06T13:18:05.230 回答