你的位置:首页 > 软件开发 > ASP.net > WPF 滑块开关 转换器 对XML的操作

WPF 滑块开关 转换器 对XML的操作

发布时间:2015-05-05 12:00:26
今天大清早起床打开微信朋友圈看到大学同学院一哥们发的结婚的照片,在此遥祝一对新人:新婚快乐,百年好合!这哥们大学时时班长,结婚也来了好多同学,不由得觉得吾等屌丝大学确实留下了很多遗憾~哦,对了,这哥们还跟我现在在一个公司的不同部门里,听说他也混的如鱼得水,祝福!转到正题,今天想记 ...

今天大清早起床打开微信朋友圈看到大学同学院一哥们发的结婚的照片,在此遥祝一对新人:新婚快乐,百年好合!这哥们大学时时班长,结婚也来了好多同学,不由得觉得吾等屌丝大学确实留下了很多遗憾~哦,对了,这哥们还跟我现在在一个公司的不同部门里,听说他也混的如鱼得水,祝福!

转到正题,今天想记录的知识点有三个:

1.get='_blank'>wpf样式实现安卓和苹果移动终端上的滑块式开关

2.wpf转换器的使用;

3.c#对;

说说需求,项目上要手动打开配置文件修改里面的值,确切来说是在true和false中间切换里面一个配置的值。想着给客户上线使用,让他们打开配置文件切换值肯定是不好的,就想着做个图形界面的开关。觉得手机终端的滑块式开关不错,就自己写了一个。先看手机上的滑块式开关是什么样的:

WPF 滑块开关 转换器 对XML的操作

对,就要这种效果。

先看XAML界面的代码吧:

 1 <Window x:Class="ModifyConfig.MainWindow" 2     ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3     ="http://schemas.microsoft.com/winfx/2006/xaml" 4     ="clr-namespace:ModifyConfig" 5     Title="MainWindow" 6     Width="525" 7     Height="350"> 8   <Window.Resources> 9  10     <local:GradeState x:Key="GradeState" /> 11      12     <Style x:Key="CheckRadioFocusVisual"> 13       <Setter Property="Control.Template"> 14         <Setter.Value> 15           <ControlTemplate> 16             <Rectangle Margin="14,0,0,0" 17                   SnapsToDevicePixels="true" 18                   Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" 19                   StrokeDashArray="1 2" 20                   StrokeThickness="1" /> 21           </ControlTemplate> 22         </Setter.Value> 23       </Setter> 24     </Style> 25     <Style x:Key="SliderCheckBox" TargetType="{x:Type CheckBox}"> 26       <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /> 27       <Setter Property="BorderThickness" Value="1" /> 28       <Setter Property="Cursor" Value="Hand" /> 29       <Setter Property="Template"> 30         <Setter.Value> 31           <ControlTemplate TargetType="{x:Type CheckBox}"> 32             <ControlTemplate.Resources> 33               <Storyboard x:Key="StoryboardIsChecked"> 34                 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckFlag" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"> 35                   <EasingDoubleKeyFrame KeyTime="0" Value="0" /> 36                   <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="14" /> 37                 </DoubleAnimationUsingKeyFrames> 38               </Storyboard> 39               <Storyboard x:Key="StoryboardIsCheckedOff"> 40                 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckFlag" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"> 41                   <EasingDoubleKeyFrame KeyTime="0" Value="14" /> 42                   <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0" /> 43                 </DoubleAnimationUsingKeyFrames> 44               </Storyboard> 45             </ControlTemplate.Resources> 46             <BulletDecorator Background="Transparent" SnapsToDevicePixels="true"> 47               <BulletDecorator.Bullet> 48                 <Border x:Name="ForegroundPanel" 49                     Width="35" 50                     Height="20" 51                     BorderThickness="1" 52                     CornerRadius="10"> 53                   <Canvas> 54                     <Border x:Name="CheckFlag" 55                         Width="19" 56                         Height="18" 57                         VerticalAlignment="Center" 58                         Background="{Binding Converter={StaticResource GradeState} }" 59                         BorderThickness="1" 60                         CornerRadius="10" 61                         RenderTransformOrigin="0.5,0.5"> 62                       <Border.RenderTransform> 63                         <TransformGroup> 64                           <ScaleTransform /> 65                           <SkewTransform /> 66                           <RotateTransform /> 67                           <TranslateTransform /> 68                         </TransformGroup> 69                       </Border.RenderTransform> 70                       <Border.Effect> 71                         <DropShadowEffect Direction="180" ShadowDepth="1" /> 72                       </Border.Effect> 73                     </Border> 74                   </Canvas> 75                 </Border> 76               </BulletDecorator.Bullet> 77               <ContentPresenter Margin="{TemplateBinding Padding}" 78                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 79                        VerticalAlignment="Center" 80                        RecognizesAccessKey="True" 81                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 82             </BulletDecorator> 83             <ControlTemplate.Triggers> 84               <Trigger Property="HasContent" Value="true"> 85                 <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}" /> 86                 <Setter Property="Padding" Value="4,0,0,0" /> 87               </Trigger> 88               <Trigger Property="IsEnabled" Value="false"> 89                 <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> 90               </Trigger> 91               <Trigger Property="IsChecked" Value="True"> 92                 <Setter TargetName="ForegroundPanel" Property="Background" Value="LightGreen" /> 93                 <Trigger.EnterActions> 94                   <BeginStoryboard x:Name="BeginStoryboardCheckedTrue" Storyboard="{StaticResource StoryboardIsChecked}" /> 95                   <RemoveStoryboard BeginStoryboardName="BeginStoryboardCheckedFalse" /> 96                 </Trigger.EnterActions> 97               </Trigger> 98               <Trigger Property="IsChecked" Value="False"> 99                 <Setter TargetName="ForegroundPanel" Property="Background" Value="LightGray" />100                 <Trigger.EnterActions>101                   <BeginStoryboard x:Name="BeginStoryboardCheckedFalse" Storyboard="{StaticResource StoryboardIsCheckedOff}" />102                   <RemoveStoryboard BeginStoryboardName="BeginStoryboardCheckedTrue" />103                 </Trigger.EnterActions>104               </Trigger>105             </ControlTemplate.Triggers>106           </ControlTemplate>107         </Setter.Value>108       </Setter>109     </Style>110   </Window.Resources>111   <Canvas>112     <TextBlock Text="是否开启考试模式:" Canvas.Left="27" Canvas.Top="12" />113     <CheckBox Width="200"114          Height="23"115          Style="{DynamicResource SliderCheckBox}" 116          x:Name="Switch_IsExamMode"117          Click="IsExamMode_Click" Canvas.Left="132" Canvas.Top="12" />118 119   </Canvas>120 </Window>

原标题:WPF 滑块开关 转换器 对XML的操作

关键词:wpf

wpf
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。