1.学习wpf 一段时间,自己写了一个圆形Button 上传代码,菜鸟初级,大家勿见笑:
<Window.Resources>
<GradientStopCollection x:Key="MyGlassGradientStopsResource">
<GradientStop Color="White"
Offset="0.2" />
<GradientStop Color="Transparent"
Offset="0.4" />
<GradientStop Color="White"
Offset="0.5" />
<GradientStop Color="Transparent"
Offset="0.75" />
<GradientStop Color="White"
Offset="0.9" />
<GradientStop Color="Transparent"
Offset="1.0" />
</GradientStopCollection>
<LinearGradientBrush x:Key="MyGlassBrushResource"
StartPoint="0,0"
EndPoint="1,1"
Opacity="1"
GradientStops="{StaticResource MyGlassGradientStopsResource}" />
<ControlTemplate TargetType="Button"
x:Key="cirbtn">
<!--<定义视觉树>-->
<Grid>
<Ellipse Opacity="1.0"
Fill="{TemplateBinding Button.Background}"
Width="{TemplateBinding Button.Width}"
Height="{TemplateBinding Control.Height}">
</Ellipse>
<Grid Margin="15,15,15,15"
x:Name="cirgrid">
<Canvas x:Name="mycavas">
<Ellipse x:Name="innerel"
Opacity="1"
Fill="{StaticResource MyGlassBrushResource}"
Width="{Binding ElementName=mycavas,Path=ActualWidth}"
Height="{Binding ElementName=mycavas,Path=ActualHeight}"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
<TransformGroup>
<RotateTransform x:Name="cubeRotateTransform" />
</TransformGroup>
</Ellipse.RenderTransform>
</Ellipse>
<Ellipse x:Name="innerel2"
Opacity="0.5"
Fill="Red"
Width="{Binding ElementName=mycavas,Path=ActualWidth}"
Height="{Binding ElementName=mycavas,Path=ActualHeight}">
</Ellipse>
</Canvas>
</Grid>
<TextBlock Name="txtBlock"
Margin="{TemplateBinding Button.Padding}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Text="{TemplateBinding Button.Content}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver"
Value="True">
<!--//创建故事板对玻璃罩进行旋转-->
<Trigger.EnterActions>
<BeginStoryboard x:Name="glassstory">
<Storyboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="cubeRotateTransform"
Storyboard.TargetProperty="Angle"
By="360"
Duration="0:0:1"
RepeatBehavior="Forever" />
</Storyboard>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
<!--<按钮离开暂停动画>-->
<Trigger Property="Button.IsMouseOver"
Value="False">
<Trigger.EnterActions>
<PauseStoryboard BeginStoryboardName="glassstory"></PauseStoryboard>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
因为在圆形按钮按下的时候会有虚线框,没办法,就让按钮的属性FocusVisualStyle 为空,也没找到其它好的办法,有懂的给俺留言说下怎么搞,谢谢,代码如下:
<Style TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle"
Value="{x:Null}" />