【WPF】Behavior的使用
如何将一个行为附加到某个元素上呢?我们可以通过自定义一个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的使用的更多相关文章
- WPF之Behavior
本文主要是以实现拖动元素作为例子. 创建Behavior: 通常这个类会继承自Behavior<T>,其中T就是此Behavior服务的对象,在此处使用的是UIElement,也就是虽有的 ...
- 【WPF】 Behavior
Hello,Behavior 引言 在看PDC-09大会的视频时,其中一篇讲利用Blend来扩展Silverlight元素的行 为,当时感觉很酷:在Blend中,将MouseDra ...
- WPF点滴(3) 行为-Behavior
为了定制个性化的用户界面,我们通常会借助于WPF强大的样式(style),修改控件属性,重写控件模板(template),样式帮助我们构建一致的个性化控件.通过样式可以调整界面的显示效果,这只是界面构 ...
- WPF自定义行为Behavior,实现双击控件复制文本
WPF引用xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity& ...
- WPF Interaction框架简介(一)——Behavior
在WPF 4.0中,引入了一个比较实用的库——Interactions,这个库主要是通过附加属性来对UI控件注入一些新的功能,除了内置了一系列比较好用的功能外,还提供了比较良好的扩展接口.本文这里简单 ...
- WPF教程十:如何使用Style和Behavior在WPF中规范视觉样式
在使用WPF编写客户端代码时,我们会在VM下解耦业务逻辑,而剩下与功能无关的内容比如动画.视觉效果,布局切换等等在数量和复杂性上都超过了业务代码.而如何更好的简化这些编码,WPF设计人员使用了Styl ...
- [No0000124]WPF 扩展控件Behavior的几种方式
一.使用Attached Dependency Property的方式 (1)定义Attached Dependency Property public static class DigitsOnly ...
- wpf.xaml.behavior
Install-Package Microsoft.Xaml.Behaviors.Wpf Remove reference to “Microsoft.Expression.Interactions” ...
- 年度巨献-WPF项目开发过程中WPF小知识点汇总(原创+摘抄)
WPF中Style的使用 Styel在英文中解释为”样式“,在Web开发中,css为层叠样式表,自从.net3.0推出WPF以来,WPF也有样式一说,通过设置样式,使其WPF控件外观更加美化同时减少了 ...
随机推荐
- [Java多线程]-ThreadLocal源码及原理的深入分析
ThreadLocal<T>类:以空间换时间提供一种多线程更快捷访问变量的方式.这种方式不存在竞争,所以也不存在并发的安全性问题. //-------------------------- ...
- sqlserver 2008连接
初次安装使用SQL server 2008时,可能会遇到无法连接到(local)的情况.那么,如何解决此问题? 工具/原料 SQL server 2008 方法/步骤 1 打开SQL se ...
- Anagrams by Stack(深度优先搜索)
ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ...
- 给APP增加RSA签名
RSA签名,Google主要用于APP的来源控制与结算.所谓的结算,也是就是控制了APP只有使用现在机子上登录的Google账户从Google市场曾经下载过该APP的才能够使用,这样也就达到了app销 ...
- 12款jQuery幻灯片插件和幻灯片特效教程
jQuery 使用简单灵活,同时还有许多成熟的插件可供选择,它可以帮助你在项目中加入一些非常好的效果.滑块和幻灯片效果是常用的内容展示方式之一,这是一种在有限的网页空间内展示系列项目时非常好的方法.今 ...
- 【CodeForces】576 B. Invariance of Tree
[题目]B. Invariance of Tree [题意]给定n个数的置换,要求使n个点连成1棵树,满足u,v有边当且仅当a[u],a[v]有边,求一种方案或无解.n<=10^5. [算法]数 ...
- 指针A - 签到题(顺序三元组)
给定一个长度为N的数组A=[A1, A2, ... AN],已知其中每个元素Ai的值都只可能是1, 2或者3. 请求出有多少下标三元组(i, j, k)满足1 ≤ i < j < k ≤ ...
- 【洛谷 P3203】 [HNOI2010]弹飞绵羊(LCT)
题目链接 把每个点和能跳到的点连边,于是就构成了一个森林. 查询操作就是该点到根的路径长度,修改操作就相当于删边再重新连边. 显然是\(LCT\)的强项. 查询时\(access(x),splay(x ...
- php常用函数——数组函数
php常用函数——数组函数
- perl6 struct2-045 EXP
测试站点: http://www.yutian.com.cn/index.action http://www.hjxzyzz.com:8088/pfw/login.action 代码如下: use v ...