WPF 行为

1.行为的定义

引用using System.Windows.Interactivity;

1.1新建类并继承Behavior

想给所有控件使用时,这样写

1.2重写OnAttached和Ondetaching方法

OnAttached:把某种事件附加到某控件上,在这里指定事件类型,如 MouseEnter MouseMove MouseLeave

Ondetaching:把某种事件从某控件上移除,要和OnAttached里面的事件对应上

 

 

1.3编写行为的代码

2.如何引用?

2.1在xaml中包含 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

2.2在控件中使用定义好的行为

<Border Width="500" Background="Transparent"  Height="30" Margin="10">
            <i:Interaction.Behaviors>
                <local:Mybehavior1/>
            </i:Interaction.Behaviors>
            </Border>

 

行为和触发器差不多,如果界面上有很多button,单独添加触发器比较麻烦,可以封装起来,让控件用

 

posted @ 2021-12-31 22:44  薛定谔的小灯泡  阅读(168)  评论(0)    收藏  举报