wpf ToggleButton自定义样式

今天在开发的过程中由于会用到wpf的ToggleButton来控制开关,于是在度娘寻觅终得一位佳人分享。

<LinearGradientBrush x:Key="ButtonNormalBackgroundFill" EndPoint="0.5,1" StartPoint="0.5,0">
			<GradientStop Color="#FFFFFFFF" Offset="0"/>
			<GradientStop Color="#FFF0F0EA" Offset="0.9"/>
		</LinearGradientBrush>
		<SolidColorBrush x:Key="ButtonBorder" Color="#FF003C74"/>
		<Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}">
			<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
			<Setter Property="Background" Value="{StaticResource ButtonNormalBackgroundFill}"/>
			<Setter Property="BorderBrush" Value="{StaticResource ButtonBorder}"/>
			<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
			<Setter Property="HorizontalContentAlignment" Value="Center"/>
			<Setter Property="VerticalContentAlignment" Value="Center"/>
			<Setter Property="Template">
				<Setter.Value>
					<ControlTemplate TargetType="{x:Type ToggleButton}">
						<ControlTemplate.Resources>
							<Storyboard x:Key="OnChecked1">
								<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path">
									<EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="40,0,0,0"/>
								</ThicknessAnimationUsingKeyFrames>
								<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path1">
									<EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="40,0,0,0"/>
								</ThicknessAnimationUsingKeyFrames>
								<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path2">
									<EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="40,0,0,0"/>
								</ThicknessAnimationUsingKeyFrames>
								<StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="displayText">
									<DiscreteStringKeyFrame KeyTime="0" Value="OFF"/>
								</StringAnimationUsingKeyFrames>
							</Storyboard>
							<Storyboard x:Key="OnUnchecked1">
								<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path">
									<EasingThicknessKeyFrame KeyTime="0" Value="40,0,0,0"/>
									<EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="0"/>
								</ThicknessAnimationUsingKeyFrames>
								<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path1">
									<EasingThicknessKeyFrame KeyTime="0" Value="40,0,0,0"/>
									<EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="0"/>
								</ThicknessAnimationUsingKeyFrames>
								<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path2">
									<EasingThicknessKeyFrame KeyTime="0" Value="40,0,0,0"/>
									<EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="0"/>
								</ThicknessAnimationUsingKeyFrames>
								<StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="displayText">
									<DiscreteStringKeyFrame KeyTime="0" Value="ON"/>
								</StringAnimationUsingKeyFrames>
								<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="displayText">
									<EasingColorKeyFrame KeyTime="0" Value="White"/>
									<EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FF63FA00"/>
								</ColorAnimationUsingKeyFrames>
							</Storyboard>
						</ControlTemplate.Resources>
						<Border CornerRadius="10" Background="#FF3B3939" Width="60" Height="20">
							<Grid>
								<TextBlock x:Name="displayText" Text="ON" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
								<Path x:Name="path2" Fill="Black">
									<Path.Data>
										<GeometryGroup>
											<GeometryGroup.Children>
												<EllipseGeometry Center="10,10" RadiusX="8.5" RadiusY="8.5"></EllipseGeometry>
											</GeometryGroup.Children>
										</GeometryGroup>
									</Path.Data>
								</Path>
								<Path x:Name="path1" Fill="#FF818080">
									<Path.Data>
										<GeometryGroup>
											<GeometryGroup.Children>
												<EllipseGeometry Center="10,10" RadiusX="7" RadiusY="7"></EllipseGeometry>
											</GeometryGroup.Children>
										</GeometryGroup>
									</Path.Data>
								</Path>
								<Path x:Name="path">
									<Path.Fill>
										<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
											<GradientStop Color="#FF807F7F" Offset="0"/>
											<GradientStop Color="#FF373636" Offset="1"/>
										</LinearGradientBrush>
									</Path.Fill>
									<Path.Data>
										<GeometryGroup>
											<GeometryGroup.Children>
												<EllipseGeometry Center="10,10" RadiusX="8" RadiusY="8"></EllipseGeometry>
											</GeometryGroup.Children>
										</GeometryGroup>
									</Path.Data>
								</Path>
							</Grid>
						</Border>
						<ControlTemplate.Triggers>
							<EventTrigger RoutedEvent="ToggleButton.Checked">
								<BeginStoryboard Storyboard="{StaticResource OnChecked1}"/>
							</EventTrigger>
							<EventTrigger RoutedEvent="ToggleButton.Unchecked">
								<BeginStoryboard x:Name="OnUnchecked1_BeginStoryboard" Storyboard="{StaticResource OnUnchecked1}"/>
							</EventTrigger>
						</ControlTemplate.Triggers>
					</ControlTemplate>
				</Setter.Value>
			</Setter>
		</Style>

我把它链接分享给大家吧。原文链接:https://blog.csdn.net/anjingyatou/article/details/17417207

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值