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.hqwc.cn/news/191260.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机网络——WLAN简解

1. WLAN的发展历程 ❓ WLAN和WIFI有什么区别。 &#x1f604; 具体来说&#xff0c;WALN是抽象的概念&#xff0c;代表这无线局域网这一类技术&#xff0c;而WIFI则是具体的具体技术标准&#xff0c;虽然在生活中&#xff0c;二者的表现是强相关的&#xff08;因为是使用的wifi…

S7-1200PLC 作为MODBUSTCP服务器通信(多客户端访问)

S7-1200PLC作为MODBUSTCP服务器端通信编程应用&#xff0c;详细内容请查看下面文章链接&#xff1a; ModbusTcp通信(S7-1200PLC作为服务器端)-CSDN博客文章浏览阅读239次。S7-200Smart plc作为ModbusTcp服务器端的通信S7-200SMART PLC ModbusTCP通信(ModbusTcp服务器)_s7-200 …

2023.11.18 - hadoop之zookeeper分布式协调服务

1.zookeeper简介 ZooKeeper概念: Zookeeper是一个分布式协调服务的开源框架。本质上是一个分布式的小文件存储系统 ZooKeeper作用: 主要用来解决分布式集群中应用系统的一致性问题。 ZooKeeper结构: 采用树形层次结构&#xff0c;没有目录与文件之分,ZooKeeper树中的每个节点被…

“流量为王”的时代一去不返!如何押注互联网下一个黄金十年

目录 1“流量为王”的时代一去不返&#xff01;如何押注互联网下一个黄金十年 2AI夺走的第一份工作竟是OpenAI CEO&#xff1f;阿尔特曼被“扫地出门”&#xff0c;网友热评&#xff1a;是被GPT-5取代了吗&#xff1f;马斯克更“毒”&#xff0c;挂出求职申请链接 3GPT-4V新玩…

人工智能基础_机器学习044_逻辑回归代码实现与手动计算概率---人工智能工作笔记0084

上面我们已经把逻辑回归的公式,以及,公式对应的图形都画画出来了,然后我们再来看看 如何用代码实现 可以看到上面是代码,咱们自己去写一下 import numpy as np from sklearn.linear_model import LogistieRegression from sklearn import datasets # 训练数据和测试数据拆分…

WeTab--颜值与实力并存的浏览器插件

一.前言 现在的浏览器花花绿绿&#xff0c;有大量的广告与信息&#xff0c;令人目不暇接。有没有一款好用的浏览器插件可以解决这个问题呢&#xff1f;我愿称WeTab为版本答案。 WeTab的界面&#xff1a; 干净又整洁。最最关键的是还有智能AI供你服务。 这个WeTabAI就像chatgp…

DB9串口引脚介绍

一、公头和母头 图片示意源于网络: 二、 每个引脚的功能定义 公头&#xff1a;所有排针式的接头&#xff08;5针朝上&#xff0c;从左到右序号依次是1~9&#xff09; 母头&#xff1a;所有插槽式的接孔&#xff08;5孔朝上&#xff0c;从右到左序号依次是1~9&#xff09; 针…

Matalab插值详解和源码

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

蓝桥杯每日一题2023.11.18

题目描述 蓝桥杯大赛历届真题 - C 语言 B 组 - 蓝桥云课 (lanqiao.cn) 题目分析 本题使用搜索&#xff0c;将每一个格子进行初始赋值方便确定是否为相邻的数&#xff0c;将空出的两个格子首先当作已经填好数值为100&#xff0c;此时从第一个格子右边的格子开始搜索&#xff…

算法通关村第十关-白银挑战数组最大K数

大家好我是苏麟 , 今天带来一道应用快排的题 . 数组中的第K个最大元素 描述 : 给定整数数组 nums 和整数 k&#xff0c;请返回数组中第 k 个最大的元素。 请注意&#xff0c;你需要找的是数组排序后的第 k 个最大的元素&#xff0c;而不是第 k 个不同的元素。 题目 : Le…

036、目标检测-锚框

之——对边缘框的简化 目录 之——对边缘框的简化 杂谈 正文 1.锚框操作 2.IoU交并比 3.锚框标号 4.非极大值抑制 5.实现 拓展 杂谈 边缘框这样一个指定roi区域的操作对卷积神经网络实际上是很不友好的&#xff0c;这可能会对网络感受野提出一些特定的要求&#xff0…