• 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    自定义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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    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
    {
        /// 
        /// MainWindow.xaml 的交互逻辑
        /// 
        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()
            {
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

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

    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    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
    {
        /// 
        /// MainWindow.xaml 的交互逻辑
        /// 
        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();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

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

    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    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
    {
        /// 
        /// MainWindow.xaml 的交互逻辑
        /// 
        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();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    二、触发器 (Triggers)

    在这里插入图片描述

  • 相关阅读:
    数据治理:元数据及元数据管理策略、方法和技术
    scipy.optimize.minimize函数介绍
    Material Design 风格的 UI 框架 Vuetify 使用初体验
    【编程题】【Scratch三级】2020.09 魔术表演“开花”
    回文数 洛谷 - P1015
    go协程栈底层讲解
    985测试工程师被吊打,学历和经验到底谁更重要?
    使用webWorker执行后台任务
    【代码随想录算法训练营】第48天 | 第九章 动态规划(九)+ 复习第20天 第六章 二叉树(四)
    怎么使用yolov8进行图片识别,分类,分割,视频追踪
  • 原文地址:https://blog.csdn.net/zzyzxb/article/details/134479423