WPF 自定义开关按钮实现

效果如下:

分别有四种用法:

  1. 第一种 文字在右边
  2. 第二种 文字在中间
  3. 第三种 文字无
  4. 第四种 一组单选类型
  5. 实现思路看代码吧,还不够很完美...

一、引用方法: 

<StackPanel
	Grid.Row="2"
	Margin="0,50,0,0"
	Orientation="Horizontal">


	<YOI:USwitchButton
		Width="70"
		Height="30"
		Margin="10"
		ActiveBackground="{DynamicResource PrimaryBlueColor}"
		ActiveForeground="{DynamicResource TextSecundaryColor}"
		ContentFalse="关闭"
		ContentTrue="开启"
		Cursor="Hand"
		FontSize="14"
		InactiveBackground="{DynamicResource TextWhiteColor}"
		SwitchBorderRadius="15"
		SwitchContentTemplate="Right" />


	<YOI:USwitchButton
		Width="80"
		Height="30"
		Margin="50,10,10,10"
		ActiveBackground="{DynamicResource PrimaryBlueColor}"
		ActiveForeground="{DynamicResource TextWhiteColor}"
		ContentFalse="关闭"
		ContentTrue="开启"
		FontSize="14"
		Foreground="{DynamicResource TextSecundaryColor}"
		InactiveBackground="{DynamicResource TextWhiteColor}"
		SwitchBorderRadius="15"
		SwitchContentTemplate="Middle" />

	<YOI:USwitchButton
		Width="70"
		Height="30"
		Margin="10"
		ActiveBackground="{DynamicResource PrimaryBlueColor}"
		ActiveForeground="{DynamicResource PrimaryGreen}"
		FontSize="14"
		Foreground="{DynamicResource TextSecundaryColor}"
		InactiveBackground="{DynamicResource TextWhiteColor}"
		SwitchBorderRadius="15" />


	<StackPanel
		Grid.Row="2"
		Margin="0,50,0,0"
		Orientation="Vertical">


		<YOI:USwitchButton
			Width="70"
			Height="30"
			Margin="10"
			ActiveBackground="{DynamicResource PrimaryBlueColor}"
			ActiveForeground="{DynamicResource PrimaryGreen}"
			FontSize="14"
			Foreground="{DynamicResource TextSecundaryColor}"
			InactiveBackground="{DynamicResource TextWhiteColor}"
			SwitchBorderRadius="15"
			SwitchType="1" />


		<YOI:USwitchButton
			Width="70"
			Height="30"
			Margin="10"
			ActiveBackground="{DynamicResource PrimaryBlueColor}"
			ActiveForeground="{DynamicResource PrimaryGreen}"
			FontSize="14"
			Foreground="{DynamicResource TextSecundaryColor}"
			InactiveBackground="{DynamicResource TextWhiteColor}"
			SwitchBorderRadius="15"
			SwitchType="1" />

	</StackPanel>

 二、语雀笔记:

https://www.yuque.com/docs/share/a26bfbb5-5c40-4d4b-9daa-06b53da041b0?# 《WPF 自定义开关按钮实现》

 

 

WPF(Windows Presentation Foundation)中,可以使用`DispatcherTimer`和`VisualStateManager`来实现八个按钮循环闪烁的效果。这里是一个简单的示例: ```csharp <Window x:Class="ButtonFlashingApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <StackPanel> <!-- 这里放置8个按钮 --> <Button Content="Button 1" /> <Button Content="Button 2" /> <!-- ...重复七次... --> </StackPanel> <!-- 创建状态管理器和定时器 --> <VisualStateManager.VisualStateGroups> <VisualStateGroup> <VisualState Name="Off" /> <VisualState Name="On" /> <Storyboard x:Name="FlashStoryboard"> <ColorAnimation Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)" To="White" Duration="0:0:0.5" AutoReverse="True" RepeatBehavior="Forever"/> </Storyboard> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <!-- 当定时器触发时改变按钮的状态 --> <VisualStateManager> <VisualStateManager.Transitions> <Transition From="Off" To="On" GeneratedDuration="0:0:0.5"> <BeginStoryboard> <Storyboard> <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.IsEnabled)"> <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding ElementName=buttonFlasher, Path=IsEnabled}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </Transition> <Transition From="On" To="Off" GeneratedDuration="0:0:0.5"> <BeginStoryboard> <Storyboard> <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.IsEnabled)"> <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding ElementName=buttonFlasher, Path=IsEnabled, Converter={StaticResource NotConverter}}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </Transition> </VisualStateManager.Transitions> </VisualStateManager> <!-- 定义一个属性来控制按钮的可见性 --> <local:ToggleButtonControl x:Name="buttonFlasher" IsChecked="{x:Null}" /> </Grid> </Window> <!-- 自定义转换器 NotConverter --> <Style TargetType="ToggleButton"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ToggleButton"> <Grid Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="False"> <Setter Property="Visibility" Value="{TemplateBinding IsChecked, Converter={StaticResource NotConverter}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!-- 转换器类 --> <local:NotConverter x:Key="NotConverter"> <local:NotConverter.Converter逻辑> <lambda:真假倒置逻辑 x:TypeArguments="System:Boolean"/> </local:NotConverter.Converter逻辑> </local:NotConverter> ``` 在这个例子中,我们创建了一个`ToggleButtonControl`自定义控件作为开关,它会切换八个按钮的可见性。`VisualStateManager`用于控制按钮的显示和隐藏,并通过`DispatcherTimer`每隔一段时间更改`ToggleButtonControl`的状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值