zoukankan      html  css  js  c++  java
  • 设置按钮的Style

    这个例子是创建一个完全自定义Style的按钮。

    1.创建名为ButtonStyleSample1的工程。

    2.选中按钮控件,并添加到页面中。

    3.为按钮创建Style,如下图

    4.为Style命名并选择代码所在位置。

    5.如下图所示,button由三个部分组成,Grid,ButtonBackground(Border类型--按钮的背景),ContentContainer(ContentControl类型--按钮文字)

    6.重新设置背景:删除ButtonBackground其他属性,并设置背景颜色(图片),圆角度数等

    7.重新设置文字颜色:reset ContentContainer控件绑定的Foreground,并设置新的Foreground

    8.还可以设置文字的位置,字体大小等其他属性

    9.这个时候你将看到你的按钮如下图所示

    10.设置按钮其他状态的显示形态(以Pressed状态为例),如下图选中State->Pressed

    11.此时可以重复步骤6,7,8设置Pressed状态下的显示形态,设置完毕如下图

    编译并运行,就ok了!

    MainPage.xaml
    <phone:PhoneApplicationPage
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone
    ="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell
    ="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d
    ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
    ="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable
    ="d" d:DesignWidth="480" d:DesignHeight="800"
    x:Class
    ="ButtonStyleSample1.MainPage"
    FontFamily
    ="{StaticResource PhoneFontFamilyNormal}"
    FontSize
    ="{StaticResource PhoneFontSizeNormal}"
    Foreground
    ="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations
    ="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible
    ="True">
    <phone:PhoneApplicationPage.Resources>
    <Style x:Key="ButtonStyle1" TargetType="Button">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
    <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
    <Setter Property="Padding" Value="10,3,10,5"/>
    <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>
    <ColorAnimation Duration="0" To="Gray" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="ButtonBackground" d:IsOptimized="True"/>
    <ColorAnimation Duration="0" To="White" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[2].(GradientStop.Color)" Storyboard.TargetName="ButtonBackground" d:IsOptimized="True"/>
    <ColorAnimation Duration="0" To="Gray" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="ButtonBackground" d:IsOptimized="True"/>
    <ColorAnimation Duration="0" To="Black" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="ContentContainer" d:IsOptimized="True"/>
    </Storyboard>
    </VisualState>
    <VisualState x:Name="Disabled"/>
    </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Border x:Name="ButtonBackground" CornerRadius="8">
    <Border.Background>
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="White" Offset="1"/>
    <GradientStop Color="White"/>
    <GradientStop Color="Gray" Offset="0.5"/>
    </LinearGradientBrush>
    </Border.Background>
    <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Foreground="Red"/>
    </Border>
    </Grid>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>
    </phone:PhoneApplicationPage.Resources>

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Button Content="Button" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource ButtonStyle1}"/>
    </Grid>
    </Grid>
    </phone:PhoneApplicationPage>


    工程:https://files.cnblogs.com/crazystars/ButtonStyleSample1.rar

  • 相关阅读:
    转:CXF学习笔记一:如何创建、发布和访问基于CXF的服务
    转:Osgi实战中的问题
    转:用Spring JMS使异步消息变得简单
    jqueryeasyui分页组件的用法
    JavaScript高级程序设计(第三版)学习笔记一
    浅谈组建开发团队
    平常心
    狮子座与摩羯座 转载
    android项目实战(一)
    chrome安装media player无效的解决方法
  • 原文地址:https://www.cnblogs.com/crazystars/p/2208020.html
Copyright © 2011-2022 走看看