Windows Phone 7中,修改button样式并重用

在Wp7中按钮的默认效果是这样的:

  黑色主题普通/按下


2000144c8-0.gif 200014N11-1.gif


  白色主题普通/按下


200014JM-2.gif 2000145014-3.gif


  普通状态的背景色前景很容已通过修改Background、Foreground、BorderBrush等属性实现

  1. <ButtonContent="Button"Height="72"HorizontalAlignment="Left"Name="button1"VerticalAlignment="Top"Width="160"

  2. Background="Red"BorderBrush="Green"Foreground="Blue"

  3. Click="button1_Click"/>

  4. ;  


200014LD-4.gif


  但是按下状态的效果并未改变,仍然是默认效果


200014N11-1.gif


  按下效果可以通过Blend修改按钮样式来实现

  默认的按钮样式是这样的

  1. <Stylex:Key="ButtonStyle1"TargetType="Button">

  2. <SetterProperty="Background"Value="Transparent"/>

  3. <SetterProperty="BorderBrush"Value="{StaticResource PhoneForegroundBrush}"/>

  4. <SetterProperty="Foreground"Value="{StaticResource PhoneForegroundBrush}"/>

  5. <SetterProperty="BorderThickness"Value="{StaticResource PhoneBorderThickness}"/>

  6. <SetterProperty="FontFamily"Value="{StaticResource PhoneFontFamilySemiBold}"/>

  7. <SetterProperty="FontSize"Value="{StaticResource PhoneFontSizeMediumLarge}"/>

  8. <SetterProperty="Padding"Value="10,3,10,5"/>

  9. <SetterProperty="Template">

  10. <Setter.Value>

  11. <ControlTemplateTargetType="Button">

  12. <GridBackground="Transparent">

  13. <VisualStateManager.VisualStateGroups>

  14. <VisualStateGroupx:Name="CommonStates">

  15. <VisualStatex:Name="Normal"/>

  16. <VisualStatex:Name="MouseOver"/>

  17. <VisualStatex:Name="Pressed">

  18. <Storyboard>

  19. <ObjectAnimationUsingKeyFramesStoryboard.TargetProperty="Foreground"Storyboard.TargetName=
    "ContentContainer">

  20. <DiscreteObjectKeyFrameKeyTime="0"Value="{StaticResource PhoneBackgroundBrush}"/>

  21. </ObjectAnimationUsingKeyFrames>

  22. <ObjectAnimationUsingKeyFramesStoryboard.TargetProperty="Background"Storyboard.TargetName="
    ButtonBackground"
    >

  23. <DiscreteObjectKeyFrameKeyTime="0"Value="{StaticResource PhoneForegroundBrush}"/>

  24. </ObjectAnimationUsingKeyFrames>

  25. <ObjectAnimationUsingKeyFramesStoryboard.TargetProperty="BorderBrush"Storyboard.TargetName=
    "ButtonBackground">

  26. <DiscreteObjectKeyFrameKeyTime="0"Value="{StaticResource PhoneForegroundBrush}"/>

  27. </ObjectAnimationUsingKeyFrames>

  28. </Storyboard>

  29. </VisualState>

  30. <VisualStatex:Name="Disabled">

  31. <Storyboard>

  32. <ObjectAnimationUsingKeyFramesStoryboard.TargetProperty="Foreground"Storyboard.TargetName="
    ContentContainer"
    >

  33. <DiscreteObjectKeyFrameKeyTime="0"Value="{StaticResource PhoneDisabledBrush}"/>

  34. </ObjectAnimationUsingKeyFrames>

  35. <ObjectAnimationUsingKeyFramesStoryboard.TargetProperty="BorderBrush"Storyboard.TargetName="
    ButtonBackground"
    >

  36. <DiscreteObjectKeyFrameKeyTime="0"Value="{StaticResource PhoneDisabledBrush}"/>

  37. </ObjectAnimationUsingKeyFrames>

  38. <ObjectAnimationUsingKeyFramesStoryboard.TargetProperty="Background"Storyboard.TargetName="
    ButtonBackground"
    >

  39. <DiscreteObjectKeyFrameKeyTime="0"Value="Transparent"/>

  40. </ObjectAnimationUsingKeyFrames>

  41. </Storyboard>

  42. </VisualState>

  43. </VisualStateGroup>

  44. </VisualStateManager.VisualStateGroups>

  45. <Borderx:Name="ButtonBackground"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="
    {TemplateBinding BorderThickness}"
    Background="{TemplateBinding Background}"CornerRadius="0"Margin="
    {StaticResource PhoneTouchTargetOverhang}"
    >

  46. <ContentControlx:Name="ContentContainer"ContentTemplate="{TemplateBinding ContentTemplate}"
    Content="{TemplateBinding Content}"Foreground="{TemplateBinding Foreground}"HorizontalContentAlignment="
    {TemplateBinding HorizontalContentAlignment}"
    Padding="{TemplateBinding Padding}"VerticalContentAlignment="
    {TemplateBinding VerticalContentAlignment}"
    />

  47. </Border>

  48. </Grid>

  49. </ControlTemplate>

  50. </Setter.Value>

  51. </Setter>

  52. </Style>

  将下<VisualState x:Name="Pressed">的Backgournd属性改成这样

  1. <DiscreteObjectKeyFrameKeyTime="0"Value="#600010"/>

  按下效果如下:


2000145359-6.gif


  如果需要加入两个按钮,他们的按下效果不完全一样,例如一个按下背景是棕色,一个是紫色就需要写两个Style。

  仅仅这点差异,就需要写一个50行的style,这对代码维护显然是不利的。最终找到了一个解决方案。步骤如下

  1. 创建一个Button的派生类

  1. publicclass ButtonEx : Button  

  2.    {

  3.        #region Fields

  4. publicstaticreadonly DependencyProperty PressedBackgroundProperty =  

  5.            DependencyProperty.Register("PressedBackground",  

  6. typeof(Brush),  

  7. typeof(ButtonEx),  

  8. new PropertyMetadata(new SolidColorBrush(Colors.White), null));  

  9. publicstaticreadonly DependencyProperty PressedForegroundProperty =  

  10.            DependencyProperty.Register("PressedForeground",  

  11. typeof(Brush),  

  12. typeof(ButtonEx),  

  13. new PropertyMetadata(new SolidColorBrush(Colors.Black), null));  

  14. publicstaticreadonly DependencyProperty PressedBorderBrushProperty =  

  15.            DependencyProperty.Register("PressedBorderBrush",  

  16. typeof(Brush),  

  17. typeof(ButtonEx),  

  18. new PropertyMetadata(new SolidColorBrush(Colors.Black), null));  

  19. publicstaticreadonly DependencyProperty InvisibleMarginProperty =  

  20.            DependencyProperty.Register("InvisibleMargin",  

  21. typeof(Thickness),  

  22. typeof(ButtonEx),  

  23. new PropertyMetadata(new Thickness(12), null));

  24.        #endregion

  25.        #region Properties

  26. public Brush PressedBackground  

  27.        {  

  28. set { SetValue(PressedBackgroundProperty, value); }  

  29. get { return (Brush)GetValue(PressedBackgroundProperty); }  

  30.        }  

  31. public Brush PressedForeground  

  32.        {  

  33. set { SetValue(PressedForegroundProperty, value); }  

  34. get { return (Brush)GetValue(PressedForegroundProperty); }  

  35.        }  

  36. public Brush PressedBorderBrush  

  37.        {  

  38. set { SetValue(PressedBorderBrushProperty, value); }  

  39. get { return (Brush)GetValue(PressedBorderBrushProperty); }  

  40.        }  

  41. public Thickness InvisibleMargin  

  42.        {  

  43. set { SetValue(InvisibleMarginProperty, value); }  

  44. get { return (Thickness)GetValue(InvisibleMarginProperty); }  

  45.        }

  46.        #endregion

  47.    }  


http://silverlightchina.net/html/zhuantixilie/winphone7/2011/1128/12144.html


你可能感兴趣的:(windows,phone,7中,修改button样式并重用)