WPF 行为的用法(Behavior)
行为的用法有些类似触发器的效果,但是触发器一般只能适用同一种的控件;而一个行为可以用在不同控件下(指定相同的父类);
示例:
注意先要添加引用System.Windows.Interactivity.dll类库(下载链接:https://download.csdn.net/download/qq_43024228/13674433)
先新建一个行为类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Effects;
namespace WpfApp1
{
public class EffectBehavior:Behavior<FrameworkElement>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.MouseMove += AssociatedObject_MouseMove;
AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
}
private void AssociatedObject_MouseLeave(object sender, System.Windows.Input

本文介绍WPF行为(Behavior)的使用方法,通过创建自定义行为类EffectBehavior实现鼠标悬停时为不同控件添加红色阴影效果。该行为适用于多种FrameworkElement类型的控件。
最低0.47元/天 解锁文章
2047

被折叠的 条评论
为什么被折叠?



