WPF中行为与触发器的概念及用法

完全来源于十月的寒流,感谢大佬讲解

一、行为 (Behaviors)

在这里插入图片描述
在这里插入图片描述

behaviors的简单测试

<Window x:Class="Test_05.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test_05"
        mc:Ignorable="d"
        xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Border x:Name="bord" Width="50" Height="50" Background="Blue">
            <b:Interaction.Behaviors>
                <b:MouseDragElementBehavior></b:MouseDragElementBehavior>
            </b:Interaction.Behaviors>
        </Border>
    </Grid>
</Window>

自定义behaviors测试

<Window x:Class="Test_05.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test_05"
        mc:Ignorable="d"
        xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Border x:Name="bord" Width="50" Height="50" Background="Blue" RenderTransformOrigin="1.47,0.858">
            <b:Interaction.Behaviors>
                <b:MouseDragElementBehavior></b:MouseDragElementBehavior>
                <local:MyBehaviors></local:MyBehaviors>
            </b:Interaction.Behaviors>
        </Border>
    </Grid>
</Window>
using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Test_05
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MyBehaviors : Behavior<Border>
    {
        protected override void OnAttached()
        {
            //AssociatedObject.Background = Brushes.Green;
            AssociatedObject.MouseEnter += (sender,args) => 
            {
                AssociatedObject.Background = Brushes.Green;
            };
            AssociatedObject.MouseLeave += (sender, args) =>
            {
                AssociatedObject.Background = Brushes.Blue;
            };
        }

        protected override void OnDetaching()
        {
        }
    }
}

点击按钮后清空某个文本框的内容

<Window x:Class="Test_05.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test_05"
        mc:Ignorable="d"
        xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <TextBox x:Name="tbox"></TextBox>
        <Button HorizontalAlignment="Left" Content="clear">
            <b:Interaction.Behaviors>
                <local:ClearTextBox Target="{Binding ElementName=tbox}"></local:ClearTextBox>
            </b:Interaction.Behaviors>
        </Button>
    </StackPanel>
</Window>
using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Test_05
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class ClearTextBox : Behavior<Button>
    {
        public TextBox Target
        {
            get { return (TextBox)GetValue(TargetProperty); }
            set { SetValue(TargetProperty, value); }
        }

        public static readonly DependencyProperty TargetProperty =
            DependencyProperty.Register("Target", typeof(TextBox), typeof(ClearTextBox), new PropertyMetadata(null));

        protected override void OnAttached()
        {
            AssociatedObject.Click += EmptyText;
        }

        private void EmptyText(object sender, RoutedEventArgs e)
        {
            Target?.Clear();
        }
    }
}

用鼠标滚轮调整文本框中的数字

<Window x:Class="Test_05.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test_05"
        mc:Ignorable="d"
        xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <TextBox x:Name="tbox" FontSize="30" Text="0">
            <b:Interaction.Behaviors>
                <local:MouseWheelBehavior MinValue="-100" MaxValue="100" Scale="3"></local:MouseWheelBehavior>
            </b:Interaction.Behaviors>
        </TextBox>
    </StackPanel>
</Window>
using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Test_05
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MouseWheelBehavior : Behavior<TextBox>
    {
        public int MaxValue { get; set; } = 10;
        public int MinValue { get; set; } = -10;
        public int Scale { get; set; } = 1;

        protected override void OnAttached()
        {
            AssociatedObject.MouseWheel += Wheel;
        }

        private void Wheel(object sender, MouseWheelEventArgs e)
        {
            int num = int.Parse(AssociatedObject.Text);

            if (e.Delta > 0)
            {
                num += Scale;
            }
            else
            {
                num -= Scale;
            }
            if (num > MaxValue)
            {
                num = MaxValue;
            }
            if (num < MinValue)
            {
                num = MinValue;
            }
            AssociatedObject.Text = num.ToString();
        }
    }
}

二、触发器 (Triggers)

在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

STL简介

> 作者简介&#xff1a;დ旧言~&#xff0c;目前大二&#xff0c;现在学习Java&#xff0c;c&#xff0c;c&#xff0c;Python等 > 座右铭&#xff1a;松树千年终是朽&#xff0c;槿花一日自为荣。 > 目标&#xff1a;了解c中的STL库 > 毒鸡汤&#xff1a;路难行&a…

22款奔驰S450L升级流星雨大灯 感受最高配的数字大灯

“流星雨”数字大灯&#xff0c;极具辨识度&#xff0c;通过260万像素的数字微镜技术&#xff0c;实现“流星雨”仪式感与高度精确的光束分布&#xff1b;在远光灯模式下&#xff0c;光束精准度更达之前84颗LED照明的100倍&#xff0c;更新增坡道照明功能&#xff0c;可根据导航…

UE 程序化网格 计算横截面 面积

首先在构造函数内加上程序化网格&#xff0c;然后复制网格体到程序化网格组件上&#xff0c;将Static Mesh&#xff08;类型StaticMeshActor&#xff09;的静态网格体组件给到程序化网格体上 然后把StaticMesh&#xff08;类型为StaticMeshActor&#xff09;Instance暴漏出去 …

原型网络Prototypical Network的python代码逐行解释,新手小白也可学会!!-----系列6 (承接系列5)

文章目录 一、原始代码---随机采样和评估模型二、详细解释分析每一行代码 一、原始代码—随机采样和评估模型 def randomSample(self,D_set): #从D_set随机取支持集和查询集&#xff08;20个类中的其中一个类&#xff0c;shape为[20,105,105]&#xff09;index_list list(ran…

复旦大学EMBA深度链接深圳科创产业:聚焦智联,产融未来

作为科创成就的经济大区&#xff0c;深圳南山区通过跨界创新研发生态链条&#xff0c;领跑科创产业创新&#xff0c;以187.5平方公里的面积&#xff0c;雄踞着204家上市公司&#xff0c;地均生产总值产出达到了40.7亿元&#xff0c;相当于每平方公里出产超过1家上市公司&#x…

Java项目实战《苍穹外卖》 二、项目搭建

当我痛苦地站在你的面前 你不能说我一无所有 你不能说我两手空空 系列文章目录 苍穹外卖是黑马程序员2023年的Java实战项目&#xff0c;作为业余练手用&#xff0c;需要源码或者课程的可以找我&#xff0c;无偿分享 Java项目实战《苍穹外卖》 一、项目概述Java项目实战《苍穹外…

B-2:Linux系统渗透提权

B-2:Linux系统渗透提权 服务器场景:Server2204(关闭链接) 用户名:hacker 密码:123456 1.使用渗透机对服务器信息收集,并将服务器中SSH服务端口号作为flag提交; 使用nmap扫描,发现ssh服务端口为2283 Flag:2283 2.使用渗透机对服务器信息收集,并将服务器中…

复合、委托、继承

1. 单例模式 静态实例对象在getInstance函数中定义&#xff0c;这样只有在调用函数时才会生成对象 2. 复合 1. 类中封装另一个类某些功能&#xff1b; 2. 构造、析构的调用过程 指明了复合中如何调用被包含类的构造函数&#xff0c;可以直接写在初始化列表位置&#xff1b; 3.…

剑指JUC原理-19.线程安全集合

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱吃芝士的土豆倪&#xff0c;24届校招生Java选手&#xff0c;很高兴认识大家&#x1f4d5;系列专栏&#xff1a;Spring源码、JUC源码&#x1f525;如果感觉博主的文章还不错的话&#xff0c;请&#x1f44d;三连支持&…

Spring Cloud -熔断器Hystrix

为什么需要服务降级或熔断 微服务架构与传统架构的一个显著区别就是服务变多了&#xff0c;任何一个服务调用失败、或者服务不可用&#xff0c;都会对整个应用造成影响。比如前段时间阿里云整体业务不可用&#xff0c;有多方猜测就是阿里云的某一个关键服务不可用导致的。 服…

火星探测器背后的人工智能:从原理到实战的强化学习

目录 一、引言二、强化学习基础强化学习的基本概念主要算法概述Q-Learning 示例代码 环境建模与奖励设计 三、火星探测器任务分析任务需求与挑战探测器环境建模目标设定与奖励机制层层递进的关系 四、强化学习模型设计模型架构概述DQN架构核心组件&#xff1a; 状态、动作与奖励…

RT-Thread基于STM32H743的网络通信调试

使用STM32H743开发网络通信&#xff0c;本以为会很简单&#xff0c;实际却遇到好多问题&#xff0c;记录一下&#xff0c;以备后续查看。 1.新建工程&#xff0c;系统版本选择的是4.1.1&#xff0c;芯片型号是STM32H743IIK6. 2.修改系统时钟&#xff0c;使用外部25MHz晶振&…

C++类与对象(1)—初步认识

目录 一、面向过程和面向对象 二、类 1、定义 2、类的两种定义方式 3、访问限定符 4、命名规范化 5、类的实例化 6、计算类对象的大小 7、存储方式 三、this指针 1、定义 2、存储位置 3、辨析 四、封装好处 一、面向过程和面向对象 C语言是面向过程的&#xf…

记录联系ThinkPad T490扬声器无声音但插耳机有声音的解决办法

型号&#xff1a;联想ThinkPad T490&#xff0c;系统Win10 64位。 现象&#xff1a;扬声器无声音&#xff0c;插耳机有声音。且右下角小喇叭正常&#xff0c;设备管理器中驱动显示一切也都正常&#xff08;无黄色小叹号&#xff09;。 解决办法&#xff1a; 尝试了各种方法&a…

基础课7——数据预处理

在智能客服系统中&#xff0c;数据预处理是进行自然语言处理&#xff08;NLP&#xff09;的关键步骤之一。它是对用户输入的文本数据进行分析、处理和转换的过程&#xff0c;目的是将原始文本数据转化为计算机可理解的语言&#xff0c;为后续的智能回答提供支持。 1.什么是数据…

数据结构-插入排序+希尔排序+选择排序

目录 1.插入排序 插入排序的时间复杂度&#xff1a; 2.希尔排序 希尔排序的时间复杂度&#xff1a; 3.选择排序 选择排序的时间复杂度&#xff1a; 所谓排序&#xff0c;就是使一串记录&#xff0c;按照其中的某个或某些关键字的大小&#xff0c;递增或递减的排列起来的…

基于SpringBoot+MyBatis-Plus的教务管理系统

基于SpringBootMyBatis-Plus的教务管理系统 教务管理系统开发技术功能模块代码结构数据库设计运行截图源码获取 教务管理系统 欢迎访问此博客&#xff0c;是否为自己的毕业设计而担忧呢&#xff1f;是否感觉自己的时间不够做毕业设计呢&#xff1f;那你不妨看一下下面的文章&a…

CUDA编程一、基本概念和cuda向量加法

目录 一、cuda编程的基本概念入门 1、GPU架构和存储结构 2、cuda编程模型 3、cuda编程流程 二、cuda向量加法实践 1、代码实现 2、代码运行和结果 有一段时间对模型加速比较感兴趣&#xff0c;其中的一块儿内容就是使用C和cuda算子优化之类一起给模型推理提速。之前一直…

2023年最新软件安装管家目录

最新软件安装管家目录 软件目录 ①【电脑办公】电脑系统&#xff08;直接安装&#xff09;Win7Win8Win10OfficeOffice激活office2003office2007office2010office2013office2016office2019office365office2021wps2021Projectproject2007project2010project2016project2019projec…

python基础练习题库实验3

题目1 编写一个程序&#xff0c;根据以下定价计算成本。 Number of itemsCost1-50每件3美元 邮费: 10美元超过50每件2美元 邮寄&#xff1a;免费 举个例子&#xff1a; 代码 items_num input("Enter the number of items: ") items_num_i int(items_num) ite…
最新文章