欧美成人精品手机在线观看_69视频国产_动漫精品第一页_日韩中文字幕网 - 日本欧美一区二区

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

WPF 怎么把checkbox改成開關(guān)樣式

freeflydom
2024年8月21日 9:6 本文熱度 810

先看一下效果吧:

isChecked = false 的時(shí)候的效果

 

isChecked = true 的時(shí)候的效果

 

 然后我們來實(shí)現(xiàn)一下這個(gè)效果吧

第一步:創(chuàng)建一個(gè)空的wpf項(xiàng)目;

第二步:在項(xiàng)目里面添加一個(gè)checkbox

    <Grid>

        <CheckBox HorizontalAlignment="Center" IsChecked="True"

                  BorderBrush="Black" VerticalAlignment="Center" 

                  Content="switch" Background="#FF00ADFF"/>

    </Grid>

這個(gè)時(shí)候的checkbox的樣子是這樣的

 第三步:在頁面中右鍵checkbox,選擇  編輯模板 ,再  編輯副本, 之后確定

 vs就會(huì)給我們自動(dòng)生成一個(gè)名為 ”CheckBoxStyle1” 的Checkbox的默認(rèn)樣式的代碼,我們通過修改默認(rèn)樣式的代碼,把普通的Checkbox變成一個(gè)開關(guān)。

 第四步:修改默認(rèn)樣式

<Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto"/>

<ColumnDefinition Width="*"/>

</Grid.ColumnDefinitions>

<Border x:Name="PART_Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" 

BorderThickness="1" Height="14" Width="25" CornerRadius="5">

<Border x:Name="SwitchBorder" BorderThickness="1" BorderBrush="Gray" Background="White" Width="10" 

Margin="1" CornerRadius="5" HorizontalAlignment="Right"/>

</Border>

<ContentPresenter x:Name="contentPresenter" Grid.Column="1" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 

Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 

VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

</Grid>

把之前的樣式代碼改成上面的代碼,trigger部分,把報(bào)錯(cuò)的部分全部刪除

這個(gè)時(shí)候,我們的開關(guān)樣式就已經(jīng)完成了

 我們現(xiàn)在需要添加一些trigger和動(dòng)畫來實(shí)現(xiàn)切換效果

第五步:添加動(dòng)畫和trigger

<Storyboard x:Key="SwitchChecked">

    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="SwitchBorder" Storyboard.TargetProperty="(FrameworkElement.Width)">

        <EasingDoubleKeyFrame KeyTime="00:00:00" Value="10"/>

        <EasingDoubleKeyFrame KeyTime="00:00:00.2500000" Value="20"/>

        <EasingDoubleKeyFrame KeyTime="00:00:00.5000000" Value="10"/>

    </DoubleAnimationUsingKeyFrames>

    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SwitchBorder" Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)">

        <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static HorizontalAlignment.Left}"/>

        <DiscreteObjectKeyFrame KeyTime="00:00:00.2500000" Value="{x:Static HorizontalAlignment.Right}"/>

        <DiscreteObjectKeyFrame KeyTime="00:00:00.5000000" Value="{x:Static HorizontalAlignment.Right}"/>

    </ObjectAnimationUsingKeyFrames>

</Storyboard>


<Storyboard x:Key="SwitchUnchecked">

    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="SwitchBorder" Storyboard.TargetProperty="(FrameworkElement.Width)">

        <EasingDoubleKeyFrame KeyTime="00:00:00" Value="10"/>

        <EasingDoubleKeyFrame KeyTime="00:00:00.2500000" Value="20"/>

        <EasingDoubleKeyFrame KeyTime="00:00:00.5000000" Value="10"/>

    </DoubleAnimationUsingKeyFrames>

    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SwitchBorder" Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)">

        <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static HorizontalAlignment.Right}"/>

        <DiscreteObjectKeyFrame KeyTime="00:00:00.2500000" Value="{x:Static HorizontalAlignment.Left}"/>

        <DiscreteObjectKeyFrame KeyTime="00:00:00.5000000" Value="{x:Static HorizontalAlignment.Left}"/>

    </ObjectAnimationUsingKeyFrames>

</Storyboard>

上面這段代碼就是表示不同狀態(tài)下的不同動(dòng)畫效果,通過改變SwitchBoder的寬度和對(duì)齊方式,就可以實(shí)現(xiàn)了

然后我們?cè)侔堰@段動(dòng)畫效果運(yùn)用到模板中,再添加3個(gè)trigger,就可以了

<ControlTemplate.Triggers>

<Trigger Property="HasContent" Value="true">

<Setter Property="FocusVisualStyle" Value="{StaticResource OptionMarkFocusVisual}"/>

<Setter Property="Padding" Value="4,-1,0,0"/>

</Trigger>

<EventTrigger RoutedEvent="{x:Static CheckBox.CheckedEvent}">

<BeginStoryboard Storyboard="{StaticResource SwitchChecked}"/>

</EventTrigger>

<EventTrigger RoutedEvent="{x:Static CheckBox.UncheckedEvent}">

<BeginStoryboard Storyboard="{StaticResource SwitchUnchecked}"/>

</EventTrigger>

<Trigger Property="IsEnabled" Value="false">

<Setter Property="Background" TargetName="PART_Border" Value="{StaticResource OptionMark.Disabled.Background}"/>

<Setter Property="BorderBrush" TargetName="PART_Border" Value="{StaticResource OptionMark.Disabled.Border}"/>

</Trigger>

<Trigger Property="IsChecked" Value="False">

<Setter Property="Background" Value="White" TargetName="PART_Border"/>

<Setter Property="HorizontalAlignment" Value="Left" TargetName="SwitchBorder"/>

</Trigger>

<Trigger Property="IsMouseOver" Value="True">

<Setter Property="BorderBrush" TargetName="PART_Border" Value="{StaticResource OptionMark.MouseOver.Border}"/>

</Trigger>


</ControlTemplate.Triggers>

到現(xiàn)在,樣式和動(dòng)畫就已經(jīng)完成了,我們?cè)侔汛a全部剪切到App.xaml這個(gè)項(xiàng)目資源文件下面,再刪掉style的命名,x:Key="CheckBoxStyle1"刪掉,

這樣子我們的項(xiàng)目里面的checkbox就都是開關(guān)的樣式了,運(yùn)行項(xiàng)目也不會(huì)報(bào)錯(cuò)啦,最后的代碼如下

<Application.Resources>


<Storyboard x:Key="SwitchChecked">

<DoubleAnimationUsingKeyFrames Storyboard.TargetName="SwitchBorder" Storyboard.TargetProperty="(FrameworkElement.Width)">

<EasingDoubleKeyFrame KeyTime="00:00:00" Value="10"/>

<EasingDoubleKeyFrame KeyTime="00:00:00.2500000" Value="20"/>

<EasingDoubleKeyFrame KeyTime="00:00:00.5000000" Value="10"/>

</DoubleAnimationUsingKeyFrames>

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SwitchBorder" Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)">

<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static HorizontalAlignment.Left}"/>

<DiscreteObjectKeyFrame KeyTime="00:00:00.2500000" Value="{x:Static HorizontalAlignment.Right}"/>

<DiscreteObjectKeyFrame KeyTime="00:00:00.5000000" Value="{x:Static HorizontalAlignment.Right}"/>

</ObjectAnimationUsingKeyFrames>

</Storyboard>


<Storyboard x:Key="SwitchUnchecked">

<DoubleAnimationUsingKeyFrames Storyboard.TargetName="SwitchBorder" Storyboard.TargetProperty="(FrameworkElement.Width)">

<EasingDoubleKeyFrame KeyTime="00:00:00" Value="10"/>

<EasingDoubleKeyFrame KeyTime="00:00:00.2500000" Value="20"/>

<EasingDoubleKeyFrame KeyTime="00:00:00.5000000" Value="10"/>

</DoubleAnimationUsingKeyFrames>

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SwitchBorder" Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)">

<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static HorizontalAlignment.Right}"/>

<DiscreteObjectKeyFrame KeyTime="00:00:00.2500000" Value="{x:Static HorizontalAlignment.Left}"/>

<DiscreteObjectKeyFrame KeyTime="00:00:00.5000000" Value="{x:Static HorizontalAlignment.Left}"/>

</ObjectAnimationUsingKeyFrames>

</Storyboard>



<Style x:Key="FocusVisual">

<Setter Property="Control.Template">

<Setter.Value>

<ControlTemplate>

<Rectangle Margin="2" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

<Style x:Key="OptionMarkFocusVisual">

<Setter Property="Control.Template">

<Setter.Value>

<ControlTemplate>

<Rectangle Margin="14,0,0,0" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

<SolidColorBrush x:Key="OptionMark.Static.Background" Color="#FFFFFFFF"/>

<SolidColorBrush x:Key="OptionMark.Static.Border" Color="#FF707070"/>

<SolidColorBrush x:Key="OptionMark.Static.Glyph" Color="#FF212121"/>

<SolidColorBrush x:Key="OptionMark.MouseOver.Background" Color="#FFF3F9FF"/>

<SolidColorBrush x:Key="OptionMark.MouseOver.Border" Color="#FF5593FF"/>

<SolidColorBrush x:Key="OptionMark.MouseOver.Glyph" Color="#FF212121"/>

<SolidColorBrush x:Key="OptionMark.Pressed.Background" Color="#FFD9ECFF"/>

<SolidColorBrush x:Key="OptionMark.Pressed.Border" Color="#FF3C77DD"/>

<SolidColorBrush x:Key="OptionMark.Pressed.Glyph" Color="#FF212121"/>

<SolidColorBrush x:Key="OptionMark.Disabled.Background" Color="#FFE6E6E6"/>

<SolidColorBrush x:Key="OptionMark.Disabled.Border" Color="#FFBCBCBC"/>

<SolidColorBrush x:Key="OptionMark.Disabled.Glyph" Color="#FF707070"/>

<Style TargetType="{x:Type CheckBox}">

<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>

<Setter Property="Background" Value="{StaticResource OptionMark.Static.Background}"/>

<Setter Property="BorderBrush" Value="{StaticResource OptionMark.Static.Border}"/>

<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>

<Setter Property="BorderThickness" Value="1"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type CheckBox}">

<Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto"/>

<ColumnDefinition Width="*"/>

</Grid.ColumnDefinitions>

<Border x:Name="PART_Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" 

BorderThickness="1" Height="14" Width="25" CornerRadius="5">

<Border x:Name="SwitchBorder" BorderThickness="1" BorderBrush="Gray" Background="White" Width="10" 

Margin="1" CornerRadius="5" HorizontalAlignment="Right"/>

</Border>

<ContentPresenter x:Name="contentPresenter" Grid.Column="1" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 

Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 

VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

</Grid>

<ControlTemplate.Triggers>

<Trigger Property="HasContent" Value="true">

<Setter Property="FocusVisualStyle" Value="{StaticResource OptionMarkFocusVisual}"/>

<Setter Property="Padding" Value="4,-1,0,0"/>

</Trigger>

<EventTrigger RoutedEvent="{x:Static CheckBox.CheckedEvent}">

<BeginStoryboard Storyboard="{StaticResource SwitchChecked}"/>

</EventTrigger>

<EventTrigger RoutedEvent="{x:Static CheckBox.UncheckedEvent}">

<BeginStoryboard Storyboard="{StaticResource SwitchUnchecked}"/>

</EventTrigger>

<Trigger Property="IsEnabled" Value="false">

<Setter Property="Background" TargetName="PART_Border" Value="{StaticResource OptionMark.Disabled.Background}"/>

<Setter Property="BorderBrush" TargetName="PART_Border" Value="{StaticResource OptionMark.Disabled.Border}"/>

</Trigger>

<Trigger Property="IsChecked" Value="False">

<Setter Property="Background" Value="White" TargetName="PART_Border"/>

<Setter Property="HorizontalAlignment" Value="Left" TargetName="SwitchBorder"/>

</Trigger>

<Trigger Property="IsMouseOver" Value="True">

<Setter Property="BorderBrush" TargetName="PART_Border" Value="{StaticResource OptionMark.MouseOver.Border}"/>

</Trigger>


</ControlTemplate.Triggers>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>


</Application.Resources>



該文章在 2024/8/21 9:07:20 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲(chǔ)管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved