我为我的按钮创建了一个自定义样式,如下所示:
<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>
如您所见,我在按钮(ButtonNumber
和ButtonCharacters
)中添加了两个新的TextBlock。现在我想为这两个TextBlocks(标有XXX和YYY)的Text
属性使用自定义值,以便它看起来像这样:
<Button Style="{StaticResource CustomButtonStyle}" ButtonNumber.Text="123" ButtonCharacters.Text="SomeText" />
我不知道!
答案 0 :(得分:2)
最好继承Button类,添加两个依赖属性(例如Text1和Text2)和该类(让我们称之为TextButton一段时间)。
然后,为TextButton设置通用模板(在/themes/generic.xaml中),在ControlTemplate中设置以下标记
<TextBlock x:Name="ButtonNumber" Text="{TemplateBinding Text1}" />
<TextBlock x:Name="ButtonCharacters" Text="{TemplateBinding Text2}" />
这将是最简单和最佳实践方式。