【WPF】Behavior的使用

时间:2021-08-01 00:40:43

如何将一个行为附加到某个元素上呢?我们可以通过自定义一个Behavior!

我们首先看一下IAttachedObject接口,Behavior默认继承之这个接口

  // 摘要:
// 供可以附加到另一个对象的对象使用的接口。
public interface IAttachedObject
{
// 摘要:
// 获得关联的对象。
//
// 备注:
// 代表此实例附加到的对象。
DependencyObject AssociatedObject { get; } // 摘要:
// 附加到指定的对象。
//
// 参数:
// dependencyObject:
// 要附加到的对象。
void Attach(DependencyObject dependencyObject);
//
// 摘要:
// 将此实例与其关联的对象分离。
void Detach();
}

下面我们自定义一个Behavior附加到ListBox元素上:

 public class SelectedItemFillBehavior : Behavior<ListBox>//把行为附加到ListBox上。ListBox被附加了下面的行为
{
// Using a DependencyProperty as the backing store for DefaultHeight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DefaultHeightProperty =
DependencyProperty.Register("DefaultHeight", typeof(double), typeof(SelectedItemFillBehavior), new UIPropertyMetadata(30.0)); // Using a DependencyProperty as the backing store for AnimationDuration. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AnimationDurationProperty =
DependencyProperty.Register("AnimationDuration", typeof(int), typeof(SelectedItemFillBehavior), new UIPropertyMetadata()); public double DefaultHeight
{
get { return (double)GetValue(DefaultHeightProperty); }
set { SetValue(DefaultHeightProperty, value); }
} public int AnimationDuration
{
get { return (int)GetValue(AnimationDurationProperty); }
set { SetValue(AnimationDurationProperty, value); }
} protected override void OnAttached()
{
base.OnAttached();
//附加行为后需要处理的事件
AssociatedObject.SelectionChanged += new SelectionChangedEventHandler(OnAssociatedObjectSelectionChanged);
} protected override void OnDetaching()
{
base.OnDetaching();
//解除的事件
AssociatedObject.SelectionChanged -= new SelectionChangedEventHandler(OnAssociatedObjectSelectionChanged);
} private void OnAssociatedObjectSelectionChanged(object sender, SelectionChangedEventArgs e)
{
double selectedItemFinalHeight = AssociatedObject.ActualHeight; Storyboard storyBoard = new Storyboard(); for (int i = ; i < AssociatedObject.Items.Count; i++)
{
ListBoxItem item = AssociatedObject.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
if (!item.IsSelected)
{
selectedItemFinalHeight -= DefaultHeight; DoubleAnimation heightAnimation = new DoubleAnimation()
{
To = DefaultHeight,
Duration = new Duration(new TimeSpan(, , , , AnimationDuration))
};
Storyboard.SetTarget(heightAnimation, item);
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(FrameworkElement.HeightProperty));
storyBoard.Children.Add(heightAnimation);
}
} selectedItemFinalHeight -= ; if (AssociatedObject.SelectedIndex >= )
{
ListBoxItem selectedItem = AssociatedObject.ItemContainerGenerator.ContainerFromIndex(AssociatedObject.SelectedIndex) as ListBoxItem; DoubleAnimation fillheightAnimation = new DoubleAnimation()
{
To = selectedItemFinalHeight,
Duration = new Duration(new TimeSpan(, , , , AnimationDuration))
}; Storyboard.SetTarget(fillheightAnimation, selectedItem);
Storyboard.SetTargetProperty(fillheightAnimation, new PropertyPath(FrameworkElement.HeightProperty));
storyBoard.Children.Add(fillheightAnimation);
} storyBoard.Begin(AssociatedObject);
}
}

这样ListBox被附加了以上行为。

【WPF】Behavior的使用的更多相关文章

  1. WPF之Behavior

    本文主要是以实现拖动元素作为例子. 创建Behavior: 通常这个类会继承自Behavior<T>,其中T就是此Behavior服务的对象,在此处使用的是UIElement,也就是虽有的 ...

  2. 【WPF】 Behavior

    Hello,Behavior   引言         在看PDC-09大会的视频时,其中一篇讲利用Blend来扩展Silverlight元素的行 为,当时感觉很酷:在Blend中,将MouseDra ...

  3. WPF点滴(3) 行为-Behavior

    为了定制个性化的用户界面,我们通常会借助于WPF强大的样式(style),修改控件属性,重写控件模板(template),样式帮助我们构建一致的个性化控件.通过样式可以调整界面的显示效果,这只是界面构 ...

  4. WPF自定义行为Behavior,实现双击控件复制文本

    WPF引用xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity& ...

  5. WPF Interaction框架简介(一)——Behavior

    在WPF 4.0中,引入了一个比较实用的库——Interactions,这个库主要是通过附加属性来对UI控件注入一些新的功能,除了内置了一系列比较好用的功能外,还提供了比较良好的扩展接口.本文这里简单 ...

  6. WPF教程十:如何使用Style和Behavior在WPF中规范视觉样式

    在使用WPF编写客户端代码时,我们会在VM下解耦业务逻辑,而剩下与功能无关的内容比如动画.视觉效果,布局切换等等在数量和复杂性上都超过了业务代码.而如何更好的简化这些编码,WPF设计人员使用了Styl ...

  7. &lbrack;No0000124&rsqb;WPF 扩展控件Behavior的几种方式

    一.使用Attached Dependency Property的方式 (1)定义Attached Dependency Property public static class DigitsOnly ...

  8. wpf&period;xaml&period;behavior

    Install-Package Microsoft.Xaml.Behaviors.Wpf Remove reference to “Microsoft.Expression.Interactions” ...

  9. 年度巨献-WPF项目开发过程中WPF小知识点汇总(原创&plus;摘抄)

    WPF中Style的使用 Styel在英文中解释为”样式“,在Web开发中,css为层叠样式表,自从.net3.0推出WPF以来,WPF也有样式一说,通过设置样式,使其WPF控件外观更加美化同时减少了 ...

随机推荐

  1. 微信jsApI及微信分享对应在手机浏览器的调用总结。

    摘录自别人的博客: 第一篇:微信内置浏览器的JsAPI(WeixinJSBridge续) 之前有写过几篇关于微信内置浏览器(WebView)中特有的Javascript API(Javascript ...

  2. vs2010 仿XCode风格的头注释宏

    Sub DocumentFileHeader() Dim star As String star = "//***************************************** ...

  3. Switch用法

    package com.cz.test; public class SwitchExample1 { /** * @param args */ public static void main(Stri ...

  4. windows初始化后做了哪些事情

    保护视力,将WINDOWS背景色变成淡绿色 绿色和蓝色对眼睛最好,建议大家在长时间用电脑后,经常看看蓝天.绿地,就能在一定程度上缓解视疲劳.同样的道理,如果我们把电脑屏幕和网页的底色变为淡淡的苹果绿, ...

  5. 【Ubuntu12&period;04】安装搜狗输入法

    我的系统版本是Ubuntu12.04 32位 卸载Ibus输入法 sudo apt-get remove ibus 注意: 安装ibus的命令是  sudo apt-get install fcitx ...

  6. Java语言基本语法(一)————关键字&amp&semi;标识符(Java语言标识符命名规范&amp&semi;Java语言的包名、类名、接口名、变量名、函数名、常量名命名规则 )

    一.关键字 关键字的定义和特点 定义:被Java语言赋予特殊含义,用做专门用途的字符串(单词). 特点:关键字中所有字母均为小写 下面列举一些常用的关键字. 用于定义数据类型的关键字:byte.sho ...

  7. c&num;中取整和取余

    "%"为取余. "/"号整型运算是取整,浮点运算时为除法运算.如54/10结果为5,54.0/10.0结果为5.4.而且取整时不进行四舍五入只取整数部分,如54 ...

  8. 副本死亡传送(&lowbar;instance&lowbar;die&lowbar;tele)

    玩家在副本中死亡时,将传送至对应的坐标 mapId 副本地图ID posId 坐标ID,对应_postion中posId

  9. 2&period;2、CDH 搭建Hadoop在安装&lpar;安装Java Development Kit&rpar;

    第2步:安装Java Development Kit 要安装Oracle JDK,您可以使用Cloudera Manager安装Cloudera提供的版本,也可以直接安装Oracle的其他版本. 继续 ...

  10. windows10下安装source insight 4&period;0&lpar;破解版&rpar;

    1.从官网下载source insight4.0版本(不用下载,在后面已经把所有需要的文件都准备好了); 2.安装source insightt4.0; 3.使用下载好的sourceinsight4. ...