自定义Button的样式(转)

Xaml
1 <Window x:Class="WpfApplication1.Window1"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="WPF Calculator"
5 Height="400"
6 Width="600"
7 ResizeMode="CanMinimize"
8 Icon="Resources\appicon.ico"
9 TextInput="OnWindowKeyDown">
10 <DockPanel Name="MyPanel">
11 <Menu DockPanel.Dock="Top" Height="26">
12 <MenuItem Header="_File">
13 <MenuItem Click="OnMenuExit" Header="Exit"/>
14 </MenuItem>
15 <MenuItem Header="_View">
16 <MenuItem Name="StandardMenu" Click="OnMenuStandard" IsCheckable="true" IsChecked="True" Header="Standard"/>
17 </MenuItem>
18 <MenuItem Header="_Help">
19 <MenuItem Click="OnMenuAbout" Header="About"/>
20 </MenuItem>
21 </Menu>
22 <Grid Name="MyGrid" Background="Wheat" ShowGridLines="False">
23 <Grid.Resources >
24 <Storyboard x:Key="playStoryboard">
25 <DoubleAnimation From="50" To="40" Duration="0:0:0.25" RepeatBehavior="1x" AutoReverse="True" Storyboard.TargetName="TB" Storyboard.TargetProperty="(Ellipse.Height)"/>
26 <DoubleAnimation From="50" To="44" Duration="0:0:0.25" RepeatBehavior="1x" AutoReverse="True" Storyboard.TargetName="TB" Storyboard.TargetProperty="(Ellipse.Width)"/>
27 </Storyboard>
28 <Style x:Key="DigitBtn" TargetType="{x:Type Button}">
29 <Setter Property="Focusable" Value="False"/>
30 <Setter Property="FontSize" Value="14pt"/>
31 <Setter Property="Margin" Value="0"/>
32 <Setter Property="Template">
33 <Setter.Value>
34 <ControlTemplate TargetType="{x:Type Button}">
35 <Grid Width="60" Height="50">
36 <Ellipse Width="57" Height="49" x:Name="TB" StrokeThickness="1"
37 Stroke="{TemplateBinding Foreground}"
38 Fill="{TemplateBinding Background}"
39 HorizontalAlignment="Center" VerticalAlignment="Center" />
40 <ContentPresenter Content="{TemplateBinding Content}"
41 HorizontalAlignment="Center"
42 VerticalAlignment="Center"/>
43 </Grid>
44
45 <ControlTemplate.Triggers>
46 <Trigger Property="IsMouseOver" Value="true">
47 <Setter TargetName="TB" Property="Ellipse.Fill" Value="Lightblue" />
48 </Trigger>
49 <Trigger Property="IsPressed" Value="true">
50 <Setter TargetName="TB" Property="Ellipse.Fill" Value="Blue" />
51 </Trigger>
52
53
54 <EventTrigger RoutedEvent="ButtonBase.Click">
55 <EventTrigger.Actions>
56 <BeginStoryboard Name="playStoryboard"
57 Storyboard="{StaticResource playStoryboard}"/>
58 </EventTrigger.Actions>
59 </EventTrigger>
60
61 </ControlTemplate.Triggers>
62 </ControlTemplate>
63 </Setter.Value>
64 </Setter>
65 </Style>
66 </Grid.Resources>
67 <Grid.ColumnDefinitions>
68 <ColumnDefinition/>
69 <ColumnDefinition/>
70 <ColumnDefinition/>
71 <ColumnDefinition/>
72 <ColumnDefinition/>
73 <ColumnDefinition/>
74 <ColumnDefinition/>
75 <ColumnDefinition/>
76 <ColumnDefinition/>
77 </Grid.ColumnDefinitions>
78 <Grid.RowDefinitions>
79 <RowDefinition/>
80 <RowDefinition/>
81 <RowDefinition/>
82 <RowDefinition/>
83 <RowDefinition/>
84 <RowDefinition/>
85 </Grid.RowDefinitions>
86
87 <Button Name="B7" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="4" Grid.Row="2" >7</Button>
88 <Button Name="B8" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="5" Grid.Row="2">8</Button>
89 <Button Name="B9" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="6" Grid.Row="2">9</Button>
90
91 <Button Name="B4" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="4" Grid.Row="3" >4</Button>
92 <Button Name="B5" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="5" Grid.Row="3" >5</Button>
93 <Button Name="B6" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="6" Grid.Row="3" >6</Button>
94
95
96 <Button Name="B1" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="4" Grid.Row="4" >1</Button>
97 <Button Name="B2" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="5" Grid.Row="4" >2</Button>
98 <Button Name="B3" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="6" Grid.Row="4" >3</Button>
99
100 <Button Name="B0" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="4" Grid.Row="5" >0</Button>
101 <Button Name="BPeriod" Click="DigitBtn_Click" Style="{StaticResource DigitBtn}" Grid.Column="5" Grid.Row="5" >.</Button>
102
103 <Button Name="BPM" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="6" Grid.Row="5" >+/-</Button>
104
105 <Button Name="BDevide" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="7" Grid.Row="2" >/</Button>
106 <Button Name="BMultiply" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="7" Grid.Row="3" >*</Button>
107 <Button Name="BMinus" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="7" Grid.Row="4" >-</Button>
108 <Button Name="BPlus" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="7" Grid.Row="5" >+</Button>
109
110
111
112 <Button Name="BSqrt" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="8" Grid.Row="2" ToolTip="Usage: 'A Sqrt'" >Sqrt</Button>
113 <Button Name="BPercent" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="8" Grid.Row="3" ToolTip="Usage: 'A % B ='" >%</Button>
114 <Button Name="BOneOver" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="8" Grid.Row="4" ToolTip="Usage: 'A 1/X'">1/X</Button>
115 <Button Name="BEqual" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="8" Grid.Row="5" >=</Button>
116
117
118 <Button Name="BC" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="8" Grid.Row="1" Grid.ColumnSpan="1" ToolTip="Clear All">C</Button>
119 <Button Name="BCE" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="7" Grid.Row="1" Grid.ColumnSpan="1" ToolTip="Clear Current Entry">CE</Button>
120
121
122 <Button Name="BMemClear" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="3" Grid.Row="2" ToolTip="Clear Memory" >MC</Button>
123 <Button Name="BMemRecall" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="3" Grid.Row="3" ToolTip="Recall Memory">MR</Button>
124 <Button Name="BMemSave" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="3" Grid.Row="4" ToolTip="Store in Memory">MS</Button>
125 <Button Name="BMemPlus" Click="OperBtn_Click" Background="Darkgray" Style="{StaticResource DigitBtn}" Grid.Column="3" Grid.Row="5" ToolTip="Add To Memory">M+</Button>
126 <TextBlock Name="BMemBox" Grid.Column="3" Grid.Row="1" Margin="10,17,10,17" Grid.ColumnSpan="2">Memory: [empty]</TextBlock>
127
128 </Grid>
129 </DockPanel>
130  </Window>
131

 

posted @ 2010-01-26 17:25  玛雅人  阅读(401)  评论(0编辑  收藏  举报