2012-07-05 54 views
0

裏面我創建了一個自定義樣式爲我的按鈕,看起來像這樣:添加的TextBlock按鈕模板

<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)內部的兩個新的TextBlocks。現在我想使用這兩個的TextBlocks的Text屬性的自定義值(標有XXX和YYY),這樣它看起來就像這樣:

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

我不知道!

回答

2

更好的是繼承Button類,添加兩個依賴屬性(例如Text1和Text2)和該類(讓我們稱它爲TextButton一段時間)。

然後,TEXT按鈕設置通用模板(在/themes/generic.xaml)和內部的控件模板,你可以使用下面的標記

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

這將是最簡單和最佳實踐方法。

+0

直到現在我還沒有這樣做。那麼你有這個鏈接或者教程嗎?謝謝! – Stacksatty 2012-07-06 13:28:48

+0

http://www.windowsphonegeek.com/articles/Creating-a-WP7-Custom-Control-in-7-Steps 使用該指南進行基本概述。 – 2012-07-06 14:59:34

+0

謝謝,這似乎是我需要的! – Stacksatty 2012-07-06 15:27:31