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

news2024/10/5 14:03:11

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

一、行为 (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.coloradmin.cn/o/1223997.html

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

相关文章

YOLOv8改进 | DAttention (DAT)注意力机制实现极限涨点

论文地址&#xff1a; DAT论文地址 官方地址&#xff1a;官方代码的地址 代码地址&#xff1a;文末有修改了官方代码BUG的代码块复制粘贴即可 一、本文介绍 本文给大家带来的是YOLOv8改进DAT(Vision Transformer with Deformable Attention)的教程&#xff0c;其发布于2022…

java.net.UnknownHostException: eureka

java.net.UnknownHostException: eureka 哦。HOST漏了 #linux /etc/hosts #windows C:\Windows\System32\drivers\etc\hosts 127.0.0.1 eureka7000 127.0.0.1 eureka7001 127.0.0.1 eureka7002

Echarts柱状图配置代码详解,含常用图例代码

一、初识柱状图 从echarts官网引入基础的柱状图后&#xff0c;可以看到他有如下的配置项。我们可以改变各个配置项的属性&#xff0c;将图例调整为我们期望的效果。 二、常用配置项 因为引入echarts图例后&#xff0c;改变图例的东西都在option配置项中&#xff0c;所以其他部…

腾讯云轻量级服务器和云服务器什么区别?轻量服务器是干什么用的

随着互联网的迅速发展&#xff0c;服务器成为了许多人必备的工具。然而&#xff0c;面对众多的服务器选择&#xff0c;我们常常会陷入纠结之中。在这篇文章中&#xff0c;我们将探讨轻量服务器和标准云服务器的区别&#xff0c;帮助您选择最适合自己需求的服务器。 腾讯云双十…

python中sklearn库在数据预处理中的详细用法,及5个常用的Scikit-learn(通常简称为 sklearn)程序代码示例

文章目录 前言1. 数据清洗&#xff1a;使用 sklearn.preprocessing 中的 StandardScaler 和 MinMaxScaler 进行数据规范化。2. 缺失值处理&#xff1a;使用 sklearn.impute 中的 SimpleImputer 来填充缺失值。3. 数据编码&#xff1a;使用 sklearn.preprocessing 中的 OneHotEn…

python中的NumPy和Pandas往往都是同时使用,NumPy和Pandas的在数据分析中的联合使用

文章目录 前言一、numpy的介绍与用法二、pandas的介绍与用法三、numpy与pandas的联合使用说明四、numpy与pandas的联合使用程序代码4.1 读取CSV文件并进行数据清洗&#xff0c;如去除NaN值4.2 矩阵操作和特征工程&#xff0c;如标准化处理4.3 使用Pandas进行数据筛选和分组聚合…

ogrinfo不是内部或者外部命令

这个是GDAL的问题&#xff0c;我是通过OSGeo4w安装的&#xff0c;出来就是这个问题&#xff0c;教程没有仔细看干。 第一次安装&#xff0c;选择express install&#xff01;&#xff01;&#xff01;&#xff01; 第一次安装&#xff0c;选择express install&#xff01;&…

代码随想录算法训练营第三十九天【动态规划part02】 | 62.不同路径、63. 不同路径 II

62.不同路径 题目链接&#xff1a; 力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台 求解思路&#xff1a; 动规五部曲 确定dp数组及其下标含义&#xff1a;dp[i][j] 表示从&#xff08;0,0&#xff09;出发&#xff0c;到&#xff08;i,j&#x…

【算法】最短路径——弗洛伊德 (Floyd) 算法

目录 1.概述2.代码实现3.扩展3.应用 1.概述 &#xff08;1&#xff09;弗洛伊德 (Floyd) 算法又称为插点法&#xff0c;是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法&#xff0c;与 Dijkstra 算法类似。该算法名称以创始人之一、1978 年图灵奖获得者、…

Matalab插值详解和源码

转载&#xff1a;Matalab插值详解和源码 - 知乎 (zhihu.com) 插值法 插值法又称“内插法”&#xff0c;是利用函数f (x)在某区间中已知的若干点的函数值&#xff0c;作出适当的特定函数&#xff0c;在区间的其他点上用这特定函数的值作为函数f (x)的近似值&#xff0c;这种方…

腾讯云服务器租用价格,腾讯云服务器价格流量怎么算?

首先&#xff0c;让我们来看看腾讯云服务器租用价格。根据您的需求不同&#xff0c;腾讯云提供了多种不同的配置选项&#xff0c;从轻量级应用服务器到高性能的GPU服务器&#xff0c;都可以满足您的需求。以下是一些常见的腾讯云服务器租用价格&#xff1a; 一、腾讯云服务器租…

VPN创建连接 提示错误 628: 在连接完成前,连接被远程计算机终止。

提示错误 628: 在连接完成前&#xff0c;连接被远程计算机终止。 需要把这个地址配置一下&#xff1a; VPN类型&#xff1a;点对点PPTP 数据加密&#xff1a;如果没有加密的话&#xff0c; 要把这个选一下。

Flask 接口

目录 前言 代码实现 简单接口实现 执行其它程序接口 携带参数访问接口 前言 有时候会想着开个一个接口来访问试试&#xff0c;这里就给出一个基础接口代码示例 代码实现 导入Flask模块&#xff0c;没安装Flask 模块需要进行 安装&#xff1a;pip install flask 使用镜…

LeetCode——字符串(Java)

字符串 简介[简单] 344. 反转字符串[简单] 541. 反转字符串 II[中等] 151. 反转字符串中的单词 简介 记录一下自己刷题的历程以及代码。写题过程中参考了 代码随想录。会附上一些个人的思路&#xff0c;如果有错误&#xff0c;可以在评论区提醒一下。 [简单] 344. 反转字符串…

AIGC创作系统ChatGPT源码,AI绘画源码,支持最新GPT-4-Turbo模型,支持DALL-E3文生图

一、AI创作系统 SparkAi创作系统是基于OpenAI很火的ChatGPT进行开发的Ai智能问答系统和Midjourney绘画系统&#xff0c;支持OpenAI-GPT全模型国内AI全模型。本期针对源码系统整体测试下来非常完美&#xff0c;可以说SparkAi是目前国内一款的ChatGPT对接OpenAI软件系统。那么如…

课程设计(毕业设计)—基于机器学习(CNN+opencv+python)的车牌识别—(可远程调试)计算机专业课程设计(毕业设计)

基于机器学习(CNNopencvpython)的车牌识别 下载本文机器学习(CNNopencvpython)的车牌识别系统完整的代码和参考报告链接&#xff08;或者可以联系博主koukou(壹壹23七2五六98)&#xff0c;获取源码和报告&#xff09;https://download.csdn.net/download/shooter7/88548767此处…

操作系统·进程同步

进程同步&#xff1a;异步环境下的一组并发进程因直接制约而互相发送消息、互相合作、互相等待&#xff0c;使得各进程按照一定的速度执行的过程。 进程同步的主要任务是使并发执行的诸进程之间能有效地共享资源和相互合作&#xff0c;使执行的结果具有可再现性。 4.1 进程同…

智慧城市指挥中心,大屏幕究竟有什么用?

目前很多地区有在兴建智慧城市的项目&#xff0c;其城市指挥中心内一般都建有一张巨大的屏幕&#xff0c;这张屏幕究竟有什么用&#xff1f;是否可以用普通的电脑显示器进行代替呢&#xff1f; 智慧城市指挥中心内的巨大屏幕是智慧城市项目中的重要组成部分&#xff0c;其作用不…

​软考-高级-系统架构设计师教程(清华第2版)【第14章 云原生架构设计理论与实践(P496~526)-思维导图】​

软考-高级-系统架构设计师教程&#xff08;清华第2版&#xff09;【第14章 云原生架构设计理论与实践&#xff08;P496~526&#xff09;-思维导图】 课本里章节里所有蓝色字体的思维导图

Springboot+vue的应急物资管理系统(有报告),Javaee项目,springboot vue前后端分离项目

演示视频&#xff1a; Springbootvue的应急物资管理系统&#xff08;有报告&#xff09;&#xff0c;Javaee项目&#xff0c;springboot vue前后端分离项目 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。…