最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解WPF如何把checkbox改成開關(guān)樣式

 更新時間:2024年11月29日 08:26:58   作者:BearHan  
這篇文章主要為大家詳細(xì)介紹了WPF如何把checkbox改成開關(guān)樣式,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以參考一下

先看一下效果吧:

isChecked = false 的時候的效果

isChecked = true 的時候的效果

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

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

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

<Grid>
        <CheckBox HorizontalAlignment="Center" IsChecked="True"
                  BorderBrush="Black" VerticalAlignment="Center" 
                  Content="switch" Background="#FF00ADFF"/>
    </Grid>

這個時候的checkbox的樣子是這樣的

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

 vs就會給我們自動生成一個名為 ”CheckBoxStyle1” 的Checkbox的默認(rèn)樣式的代碼,我們通過修改默認(rèn)樣式的代碼,把普通的Checkbox變成一個開關(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)錯的部分全部刪除

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

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

第五步:添加動畫和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)下的不同動畫效果,通過改變SwitchBoder的寬度和對齊方式,就可以實(shí)現(xiàn)了

然后我們再把這段動畫效果運(yùn)用到模板中,再添加3個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)在,樣式和動畫就已經(jīng)完成了,我們再把代碼全部剪切到App.xaml這個項(xiàng)目資源文件下面,再刪掉style的命名,x:Key="CheckBoxStyle1"刪掉,

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

<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>

到此這篇關(guān)于詳解WPF如何把checkbox改成開關(guān)樣式的文章就介紹到這了,更多相關(guān)WPF把checkbox改成開關(guān)樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • C#訪問SqlServer設(shè)置鏈接超時的方法

    C#訪問SqlServer設(shè)置鏈接超時的方法

    這篇文章主要介紹了C#訪問SqlServer設(shè)置鏈接超時的方法,涉及CommandTimeout屬性的相關(guān)設(shè)置技巧,非常簡單實(shí)用,需要的朋友可以參考下
    2015-06-06
  • C#實(shí)現(xiàn)關(guān)機(jī)重啟及注銷實(shí)例代碼

    C#實(shí)現(xiàn)關(guān)機(jī)重啟及注銷實(shí)例代碼

    這篇文章主要介紹了C#實(shí)現(xiàn)關(guān)機(jī)重啟及注銷實(shí)例代碼,適合新手參考學(xué)習(xí)之用,需要的朋友可以參考下
    2014-07-07
  • c# unmanaged 約束的具體使用

    c# unmanaged 約束的具體使用

    本文主要介紹了c# unmanaged 約束的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-03-03
  • 使用C#程序驗(yàn)證系統(tǒng)登錄用戶與目錄權(quán)限

    使用C#程序驗(yàn)證系統(tǒng)登錄用戶與目錄權(quán)限

    這篇文章主要介紹了使用C#程序驗(yàn)證系統(tǒng)登錄用戶與目錄權(quán)限,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C#實(shí)現(xiàn)讀寫ini文件類實(shí)例

    C#實(shí)現(xiàn)讀寫ini文件類實(shí)例

    這篇文章主要介紹了C#實(shí)現(xiàn)讀寫ini文件類,實(shí)例分析了C#實(shí)現(xiàn)針對ini文件的讀、寫、刪除等操作的常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • Unity實(shí)現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量

    Unity實(shí)現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • c# 繪制中國象棋棋盤與棋子

    c# 繪制中國象棋棋盤與棋子

    這篇文章主要介紹了c# 繪制中國象棋棋盤與棋子,文中實(shí)例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#中通過反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法

    C#中通過反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法

    本文主要介紹了C#中通過反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C#實(shí)現(xiàn)winform漸變效果的方法

    C#實(shí)現(xiàn)winform漸變效果的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)winform漸變效果的方法,涉及到窗體的設(shè)計(jì)與屬性的修改等技巧,需要的朋友可以參考下
    2014-10-10
  • Winform窗體傳值的方法(示例)

    Winform窗體傳值的方法(示例)

    C#開發(fā)windows應(yīng)用程序項(xiàng)目時,不同窗口之間傳值有很多中方法,在此給大家介紹兩種比較常用的winform窗體傳值的方法,有需要的朋友可以參考下
    2015-08-08

最新評論

定南县| 特克斯县| 旬阳县| 麻阳| 长海县| 金坛市| 天镇县| 柞水县| 塔河县| 固原市| 石林| 司法| 崇州市| 兴化市| 台中市| 务川| 南川市| 普陀区| 澄城县| 镇雄县| 普定县| 江川县| 永顺县| 页游| 太保市| 桐梓县| 博罗县| 昭通市| 乌兰察布市| 德格县| 静海县| 乐陵市| 寿阳县| 虞城县| 绿春县| 佛教| 招远市| 宜良县| 体育| 枞阳县| 原阳县|