如何将一个行为附加到某个元素上呢?我们可以通过自定义一个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. [No0000124]WPF 扩展控件Behavior的几种方式

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

  8. wpf.xaml.behavior

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

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

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

随机推荐

  1. Ngingx--location匹配顺序

      location = /  精确匹配 /,后面不能带任何字符 location /     所有地址都是以 / 开头,所以这条规则将会匹配到所有请求.但优先级最低. location /docum ...

  2. 利用Tensorflow读取二进制CIFAR-10数据集

    使用Tensorflow读取CIFAR-10二进制数据集 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 Tensorflow官方文档 tf.transpose函数解析 tf.sli ...

  3. CSS3笔记-加强版

    属性选择器:   E[attr]只使用属性名,但没有确定任何属性值 E[attr="value"]指定属性名,并指定了该属性的属性值 E[attr~="value&quo ...

  4. Codechef Dynamic Trees and Queries

    Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...

  5. [Luogu 1967] NOIP2013 货车运输

    [Luogu 1967] NOIP2013 货车运输 一年多前令我十分头大的老题终于可以随手切掉了- 然而我这码风又变毒瘤了,我也很绝望. 看着一年前不带类不加空格不空行的清纯码风啊,时光也好像回去了 ...

  6. IDEA不显示更新、提交按钮

    问题描述: IDEA右上角不显示版本管理的“更新”.“提交”等按钮,左侧代码树中,也没有文件的状态 解决办法: 点击VCS-->Enable Version Control Integratio ...

  7. D - Keiichi Tsuchiya the Drift King Gym - 102028D (几何)

    题目链接:https://cn.vjudge.net/contest/275150#problem/D 题目大意: 问你能满足那个矩形可以顺利通过的条件,然后求出最小的w 具体思路:首先,我们应该将情 ...

  8. 64_o1

    OCE-devel-0.18.1-1.fc26.i686.rpm 15-May-2017 18:37 5634474 OCE-devel-0.18.1-1.fc26.x86_64.rpm 15-May ...

  9. PXC加入新节点避免SST时grastate.dat文件内容的修改问题

    PXC加入新节点避免SST时grastate.dat文件内容的修改问题 在主从同步完成并关闭实例后,需要修改grastate.dat中的seqno:到底应该填已经执行过最后的XID号(Executed ...

  10. 15个你不得不知道的Chrome dev tools的小技巧

    转载自:https://www.imooc.com/article/2559 谷歌浏览器如今是Web开发者们所使用的最流行的网页浏览器.伴随每六个星期一次的发布周期和不断扩大的强大的开发功能,Chro ...