当前位置: 首页 > news >正文

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.lryc.cn/news/234820.html

相关文章:

  • 2023-2024华为ICT大赛-计算赛道-广东省省赛初赛-高职组-部分赛题分析【2023.11.18】
  • 『 MySQL数据库 』数据库之表的约束
  • flink 8081 web页面无法被局域网内其他机器访问
  • 零基础安装分布式数据服务注册系统
  • 2023最新最全【OpenMV】 入门教程
  • 【Java并发编程三】线程的基本使用一
  • 企业邮箱认证指南:安全与高效的邮箱认证方法
  • Django(八、如何开启事务、介绍长见的字段类型和参数)
  • 机器学习第5天:多项式回归与学习曲线
  • MSYS2介绍及工具安装
  • Swift开发中:非逃逸闭包、逃逸闭包、自动闭包的区别
  • 栈结构应用-进制转换-辗转相除法
  • 【Azure 架构师学习笔记】-Azure Storage Account(6)- File Layer
  • idea 环境搭建及运行java后端源码
  • 掌握Shell:从新手到编程大师的Linux之旅
  • 有重复元素的快速排序
  • Bert浅谈
  • 产品运营的场景和运营策略
  • C#异常捕获try catch详细介绍
  • 切换阿里云ES方式及故障应急处理方案
  • CTFhub-RCE-过滤空格
  • 无需添加udid,ios企业证书的自助生成方法
  • 【PTA题目】6-20 使用函数判断完全平方数 分数 10
  • Nas搭建webdav服务器并同步Zotero科研文献
  • 一句话总结敏捷实践中不同方法
  • 【数据结构】线段树(点修区查)
  • Ansys Lumerical | 用于增强现实系统的表面浮雕光栅
  • QT day3作业
  • 【Ubuntu】设置永不息屏与安装 dconf-editor
  • gRPC 的原理 介绍带你从头了解gRPC