WPF之可翻转面板

news2024/10/7 2:31:37

1,创建翻转面板的资源字典:FlippPanel.xaml。

  • 无外观控件同样必须给样式指定类型( <ControlTemplate TargetType="ss:FlipPanel">),相关详情参考:WPF之创建无外观控件-CSDN博客)。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ss="clr-namespace:无外观控件"
                    xmlns:local="clr-namespace:无外观控件.Themes">
    <Style TargetType="ss:FlipPanel">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ss:FlipPanel">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"></RowDefinition>
                            <RowDefinition Height="auto"></RowDefinition>
                        </Grid.RowDefinitions>
                        <!--1,为给模板添加VisualStateManager元素,模板必须使用布局面板。布局面板包含控件的两个可视化对象和VisualStateManager元素(该元素不可见)-->
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup Name="ViewStates">
                                <VisualStateGroup.Transitions>
                                    <!--两个可视对象切换时间,以及伴随的ToggleButton切换动画-->
                                    <VisualTransition To="Normal" GeneratedDuration="00:00:01">
                                        <Storyboard >
                                            <DoubleAnimation  To="0" Storyboard.TargetName="PART_Rota" Storyboard.TargetProperty="Angle" ></DoubleAnimation>
                                        </Storyboard>
                                    </VisualTransition>
                                    <VisualTransition To="Flipped" GeneratedDuration="00:00:2">
                                        <Storyboard >
                                            <DoubleAnimation  To="180" Storyboard.TargetName="PART_Rota" Storyboard.TargetProperty="Angle" ></DoubleAnimation>
                                        </Storyboard>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>
                                <VisualState Name="Normal">
                                    <Storyboard >
                                        <DoubleAnimation To="0" Storyboard.TargetName="front" Storyboard.TargetProperty="Opacity" Duration="00:00:00"></DoubleAnimation>
                                        <!--ToggleButton旋转动画不能省,否则动画异常-->
                                        <DoubleAnimation  To="0"  Storyboard.TargetName="PART_Rota" Storyboard.TargetProperty="Angle"></DoubleAnimation>
                                    </Storyboard>
                                </VisualState>
                                <VisualState Name="Flipped">
                                    <Storyboard >
                                        <DoubleAnimation To="0" Storyboard.TargetName="back" Storyboard.TargetProperty="Opacity" Duration="00:00:00"></DoubleAnimation>
                                        <!--ToggleButton旋转动画不能省,否则动画异常-->
                                        <DoubleAnimation  To="180" Storyboard.TargetName="PART_Rota" Storyboard.TargetProperty="Angle" Duration="00:00:00" ></DoubleAnimation>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border  x:Name="front" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="{TemplateBinding CornerRadius}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                            <ContentPresenter Content="{TemplateBinding FrontContent}"></ContentPresenter>
                        </Border>
                        <Border x:Name="back" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="{TemplateBinding CornerRadius}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                            <ContentPresenter Content="{TemplateBinding BackContent}"></ContentPresenter>
                        </Border>
                        <ToggleButton  Grid.Row="1" Height="40" Name="FlipButton" RenderTransformOrigin="0.5,0.5">
                            <ToggleButton.RenderTransform>
                                <RotateTransform x:Name="PART_Rota" ></RotateTransform>
                            </ToggleButton.RenderTransform>
                            <ToggleButton.Template>
                                <ControlTemplate TargetType="ToggleButton">
                                    <ToggleButton Grid.Column="1" Grid.Row="1"  Name="FlipButton">
                                        <ToggleButton.Template>
                                            <ControlTemplate TargetType="ToggleButton">
                                                <Rectangle >
                                                    <Rectangle.Fill>
                                                        <DrawingBrush Stretch="None">
                                                            <DrawingBrush.Drawing>
                                                                <GeometryDrawing Brush="White">
                                                                    <GeometryDrawing.Pen>
                                                                        <Pen Brush="Black" Thickness="2"></Pen>
                                                                    </GeometryDrawing.Pen>
                                                                    <GeometryDrawing.Geometry>
                                                                        <GeometryGroup>
                                                                            <EllipseGeometry RadiusX="15" RadiusY="15"></EllipseGeometry>
                                                                            <CombinedGeometry GeometryCombineMode="Intersect">
                                                                                <CombinedGeometry.Geometry1>
                                                                                    <EllipseGeometry RadiusX="7.5" RadiusY="7.5"></EllipseGeometry>
                                                                                </CombinedGeometry.Geometry1>
                                                                                <CombinedGeometry.Geometry2>
                                                                                    <PathGeometry   Figures="M-7.5,0 L0,-7.5 L7.5,-7.5 L0,0 L7.5,7.5 L0,7.5 Z">
                                                                                    </PathGeometry>
                                                                                </CombinedGeometry.Geometry2>
                                                                            </CombinedGeometry>
                                                                        </GeometryGroup>
                                                                    </GeometryDrawing.Geometry>
                                                                </GeometryDrawing>
                                                            </DrawingBrush.Drawing>
                                                        </DrawingBrush>
                                                    </Rectangle.Fill>
                                                </Rectangle>
                                            </ControlTemplate>
                                        </ToggleButton.Template>
                                    </ToggleButton>
                                </ControlTemplate>
                            </ToggleButton.Template>
                        </ToggleButton>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
  • VisualStateManager只能在布局面板下进行状态管理。

2,在generic.xaml中添加资源字典FlipPanel.xaml.

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="无外观控件;component/Themes/colorpicker.xaml">
        </ResourceDictionary>
        <ResourceDictionary Source="无外观控件;component/Themes/FlipPanel.xaml"></ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

3,编写代码

 [TemplatePart(Name = "FlipButton", Type =typeof(ToggleButton))]//该特性只是进行提示,无其他意义,可舍去
    [TemplateVisualState(GroupName = "Normal", Name = "ViewStates")]//该特性提示存在可视化切换,无其他实际意义,可舍去
    [TemplateVisualState(GroupName = "Flipped", Name = "ViewStates")]
    public class FlipPanel : Control
    {
        public static readonly DependencyProperty CornerRadiusProperty;
        public static readonly DependencyProperty FrontContentProperty;
        public static readonly DependencyProperty BackContentProperty;
        public static readonly DependencyProperty IsFlippedProperty;
        static FlipPanel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FlipPanel), new FrameworkPropertyMetadata(typeof(FlipPanel)));
            CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(FlipPanel));
            FrontContentProperty = DependencyProperty.Register("FrontContent", typeof(object), typeof(FlipPanel));
            BackContentProperty = DependencyProperty.Register("BackContent", typeof(object), typeof(FlipPanel));
            IsFlippedProperty = DependencyProperty.Register("IsFlipped", typeof(bool), typeof(FlipPanel));
        }
        /// <summary>
        /// 设置控件边框倒角
        /// </summary>
        public CornerRadius CornerRadius
        {
            get
            {
                return (CornerRadius)this.GetValue(CornerRadiusProperty);
            }
            set
            {
                this.SetValue(CornerRadiusProperty, value);
            }
        }
        /// <summary>
        /// 前置内容
        /// </summary>
        public object FrontContent
        {
            get
            {
                return this.GetValue(FrontContentProperty);
            }
            set
            {
                this.SetValue(FrontContentProperty, value);
            }
        }
        /// <summary>
        /// 后置内容
        /// </summary>
        public object BackContent
        {
            get
            {
                return GetValue(BackContentProperty);
            }
            set
            {
                this.SetValue(BackContentProperty, value);
            }
        }
        /// <summary>
        /// 是否翻转
        /// </summary>
        public bool IsFlipped
        {
            get
            {
                return (bool)GetValue(IsFlippedProperty);
            }
            set
            {
                SetValue(IsFlippedProperty, value);
                ChangeVisualState(true);
            }
        }
        public override void OnApplyTemplate()
        {
            ToggleButton btn = GetTemplateChild("FlipButton") as ToggleButton;
            btn.Click += Btn_Click;
            ChangeVisualState(false);
            base.OnApplyTemplate();
           
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            IsFlipped = !IsFlipped;
           
        }
        void ChangeVisualState(bool useTransition)
        {
            if (IsFlipped)
            {
                VisualStateManager.GoToState(this, "Flipped", useTransition);
            }
            else
            {
                VisualStateManager.GoToState(this, "Normal", useTransition);
            }

        }
    }

4,在UI上添加控件

<local:FlipPanel Grid.Row="1" IsFlipped="True">
            <local:FlipPanel.FrontContent>
                <StackPanel>
                    <Button Content="前1"></Button>
                    <Button Content="前2"></Button>
                    <Button Content="前3"></Button>
                    <Button Content="前3"></Button>
                    <Button Content="前4"></Button>
                </StackPanel>
            </local:FlipPanel.FrontContent>
            <local:FlipPanel.BackContent>
                <StackPanel>
                    <Button Content="后1"></Button>
                    
                </StackPanel>
            </local:FlipPanel.BackContent>
        </local:FlipPanel>

5,效果

6,Demo 链接

https://download.csdn.net/download/lingxiao16888/89253829?spm=1001.2014.3001.5501

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1640047.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Codeforces Round 943 (Div. 3) (A-G1) C++题解

目录 比赛链接 : A. Maximize? B. Prefiquence C. Assembly via Remainders D. Permutation Game E. Cells Arrangement F. Equal XOR Segments G1. Division LCP (easy version) G2. Division LCP (hard version) 比赛链接 : Dashboard - Codeforces Round 943 (…

【Spring】Spring中AOP的简介和基本使用,SpringBoot使用AOP

&#x1f4dd;个人主页&#xff1a;哈__ 期待您的关注 目录 一、AOP简介 二、AOP个人浅谈 三、AOP中几个核心的方法注解 四、AOP中几个核心的属性 1.切入点&#xff08;PointCut&#xff09; 五、代码演示 1.SpringBoot引入依赖 2.定义一个AOP&#xff0c;也就是切面…

【数据库主从架构】

【数据库主从架构】 1. 什么是数据库的主从架构1.1 主从复制1.1.1 MySQL的主从主从复制技术三级目录 1. 什么是数据库的主从架构 随着公司业务线的增多&#xff0c;各种数据都在迅速增加&#xff0c;并且数据的读取流量也大大增加&#xff0c;就面临着数据安全问题&#xff0c;…

ICode国际青少年编程竞赛- Python-1级训练场-基础训练1

ICode国际青少年编程竞赛- Python-1级训练场-基础训练1 1、 Dev.step(4)2、 Dev.step(-4) Dev.step(8)3、 Dev.turnLeft() Dev.step(4)4、 Dev.step(3) Dev.turnLeft() Dev.step(-1) Dev.step(4)5、 Dev.step(-1) Dev.step(3) Dev.step(-2) Dev.turnLeft() Dev.step(…

Servlet(一些实战小示例)

文章目录 一、实操注意点1.1 代码修改重启问题1.2 Smart Tomcat的日志1.3 如何处理错误 一. 抓自己的包二、构造一个重定向的响应&#xff0c;让页面重定向到百度主页三、让服务器返回一个html数据四、表白墙4.1 约定前后端数据4.2 前端代码4.3 后端代码4.4 保存在数据库的版本…

超强动画制作软件blender

blender中文手册&#xff1a;Blender 4.1 Manual Blender 是一款集3D建模、渲染、动画、视频编辑、音频处理、游戏设计等多功能于一体的软件。由于其开源性质&#xff0c;它拥有庞大的用户群体和活跃的开发者社区&#xff0c;这使得Blender的功能和性能得到了不断的提升和优化…

Linux CentOS7部署ASP.NET Core应用程序,并配置Nginx反向代理服务器和Supervisor守护服务

前言&#xff1a; 本篇文章主要讲解的是如何在Linux CentOS7操作系统搭建.NET Core运行环境并发布ASP.NET Core应用程序&#xff0c;以及配置Nginx反向代理服务器。因为公司的项目一直都是托管在Window服务器IIS上&#xff0c;对于Linux服务器上托管.NET Core项目十分好奇。因为…

hot100 -- 二叉树(上)

目录 &#x1f382;二叉树的中序遍历 AC 递归 AC 迭代 &#x1f33c;二叉树的最大深度 AC DFS递归 AC BFS &#x1f6a9;翻转二叉树 AC 后序&#xff08;递归&#xff09; AC 中序 &#x1f6a9;对称二叉树 AC 递归 AC 迭代 &#x1f33c;二叉树的直径 A…

C语言之位操作符:<<、>>、、|、^、~,以及原码反码补码和例题详解

目录 前言 一、原码、反码、补码 二、移位操作符 三、位操作符&#xff1a;&、|、^、~ 四、经典例题分析&#xff1a; 总结 前言 本文将详细介绍C语言中左移操作符<<&#xff0c;右移操作符>>&#xff0c;按位与&&#xff0c;按位或|&#xff0c;按位异或^…

VS(Visual Studio)中查找项目里的中文字符

目录 正则表达式查找中文字符 正则表达式查找中文字符 在Visual Studio (VS) 中查找所有的中文字符&#xff0c;你可以使用其强大的查找和替换功能。不过&#xff0c;由于中文字符的范围非常广泛&#xff08;包括简体中文、繁体中文、日本汉字、韩国汉字等&#xff09;&#xf…

C语言——小知识和小细节17

一、未能给指针成功赋值 #include <stdio.h> #include <stdlib.h> #include <string.h>void GetMemory(char* p) {p (char*)malloc(20 * sizeof(char)); }void Test() {char* str NULL;GetMemory(str);strcpy(str, "Hello World!");printf(&quo…

计算机网络chapter2——应用层

文章目录 第2章 应用层章节引出—— 2.1应用层协议原理2.1.1 网络应用程序体系结构&#xff08;1&#xff09;客户-服务器体系结构&#xff08;2&#xff09;对等(P2P)体系结构2.1.2 进程通信1.客户和服务器进程2.进程与计算机网络之间的接口3. 进程寻址 2.1.3 可供应用程序使用…

Linux shell编程学习笔记48:touch命令

0 前言 touch是csdn技能树Linux基础练习题中最常见的一条命令&#xff0c;这次我们就来研究它的功能和用法。 1. touch命令的功能、格式和选项说明 我们可以使用命令 touch --help 来查看touch命令的帮助信息。 purpleEndurer bash ~ $ touch --help Usage: touch [OPTION]…

双指针(C++)

文章目录 1、移动零2、复写零3、快乐数4、盛最多水的容器5、有效三角形的个数6、和为s的两个数7、三数之和8、四数之和 需要理解的是&#xff0c;双指针并非只有指针&#xff0c;双指针的意思是两个位置。比如对于数组来说&#xff0c;两个下标也是双指针。当然&#xff0c;也可…

基础IO认识

回顾文件 我们之前认识文件只是在语言程度上理解&#xff0c;但是我们理解的不够彻底&#xff0c;要想真正理解文件要在os上理解。 简单代码认识 1 #include<stdio.h>2 int main(){3 FILE* fpfopen("log.txt","w");4 if(fpNULL){5 p…

【小浩算法 BST与其验证】

BST与其验证 前言我的思路思路一 中序遍历判断数组无重复递增思路二 递归边界最大值最小值的传递 我的代码测试用例1测试用例2 前言 BST是二叉树一个经典应用&#xff0c;我们常常将其用于数据的查找以及构建平衡二叉树等。今天我所做的题目是验证一颗二叉树是否为二叉搜索树&…

Web,Sip,Rtsp,Rtmp,WebRtc,专业MCU融屏视频混流会议直播方案分析

随着万物互联&#xff0c;视频会议直播互动深入业务各方面&#xff0c;主流SFU并不适合管理&#xff0c;很多业务需要各种监控终端&#xff0c;互动SIP硬件设备&#xff0c;Web在线业务平台能相互融合&#xff0c;互联互通&#xff0c; 视频混流直播&#xff0c;录存直播推广&a…

带环链表问题

带环链表就是字面意思带环的链表&#xff0c;例如以下这三种情况 练习题 1.给定一个链表&#xff0c;判断链表中是否带环. - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a;快慢指针&#xff0c;慢指针走一步&#xff0c;快指针走两步&#xff0c;两个指针从链表的起…

硅片和SOI哪个研究方向更好?

知识星球&#xff08;星球名&#xff1a;芯片制造与封测社区&#xff0c;星球号&#xff1a;63559049&#xff09;里的学员问&#xff1a;我研一将要结束&#xff0c;即将进入课题组。我们课题组方向有硅片和soi两种方向&#xff0c;这两种方向该如何选择呢&#xff1f; 硅片与…

python离线安装包的方法

python离线安装包的方法 访问对应安装包的镜像文件的网站找到适合自己的whl文件安装 访问对应安装包的镜像文件的网站 https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/<包名>/找到适合自己的whl文件 安装 下载完成后&#xff0c;进入opencv_python-3.4.11.45-c…