Behavior is something that we have know very good in the past that we can build something into the WPF system. There is Behavior class where can use as a base calss if we want to build our own behavior entities.
From its definition, you can see that
And we will demonstrate the use of Behavior class with the following examples
we have some data model where we want to display the table with some ListView, while we don't want to hard-coded the headers information because the table's metadata can change. so we will make a ListView behavior.
so , we first define some helper classes. this is for the DependencyProperty helper classes.
using System; using System.ComponentModel; using System.Windows; using System.Windows.Data; namespace BehaviorBaseClass.Behaviors { public static class DependencyPropertyExtensions { // Use Use this method with extreme caution - it can lead to memory leaks in case if you don't call RemovePropertyListener! public static void SetPropertyListener( this DependencyProperty property, object component, EventHandler handler) { var descriptor = DependencyPropertyDescriptor.FromProperty(property, component.GetType()); if (descriptor != null) descriptor.AddValueChanged(component, handler); } public static void RemovePropertyListener( this DependencyProperty property, object component, EventHandler handler) { var descriptor = DependencyPropertyDescriptor.FromProperty(property, component.GetType()); if (descriptor != null) descriptor.RemoveValueChanged(component, handler); } public static void RefreshPropertyValueFromBinding( this DependencyProperty property, DependencyObject component) { var binding = BindingOperations.GetBindingExpressionBase(component, property); if (binding != null) binding.UpdateTarget(); } } }
and below is the ListViewBehaivour classes.
using System; using System.Collections; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Interactivity; namespace BehaviorBaseClass.Behaviors { public class ListViewBehaviour : Behavior<ListView> { public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register("DataSource", typeof (IEnumerable), typeof (ListViewBehaviour), new PropertyMetadata(OnDataSourceChanged)); private ICollectionView _dataCollectionView; public IEnumerable DataSource { get { return (IEnumerable) GetValue(DataSourceProperty); } set { SetValue(DataSourceProperty, value); } } protected ICollectionView DataCollectionView { get { return _dataCollectionView; } } protected static void OnDataSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var behavior = d as ListViewBehaviour; if (behavior == null) return; behavior.OnDataCollectionChanged(GetDefaultCollectionView(e.NewValue as IEnumerable)); } protected virtual void OnDataCollectionChanged(ICollectionView newView = null) { _dataCollectionView = newView; PopulateColumns(); } protected static ICollectionView GetDefaultCollectionView(IEnumerable enumerable) { if (enumerable == null) return null; return CollectionViewSource.GetDefaultView(enumerable); } private void PopulateColumns() { var gridView = AssociatedObject.View as GridView; var collection = _dataCollectionView as ICollectionView; if (gridView != null && collection != null) { gridView.Columns.Clear(); foreach (var col in collection) { var statusColumn = new GridViewColumn() { Header = col, DisplayMemberBinding = new Binding(col.ToString()) }; gridView.Columns.Add(statusColumn); } } } private ICollectionView GetCollectionView() { return GetDefaultCollectionView(DataSource) ?? AssociatedObject.Items; } protected override void OnAttached() { base.OnAttached(); ICollectionView view = GetCollectionView(); ItemsControl.ItemsSourceProperty.SetPropertyListener(AssociatedObject, OnAssociatedObjectItemsSourceChanged); } private void OnAssociatedObjectItemsSourceChanged(object sender, EventArgs e) { PopulateColumns(); } protected override void OnDetaching() { OnDataCollectionChanged(); } } }
As you can tell, you have to make your behavior class inherits from the Behavior<T> where the T is the Decorated classes, since we will decorate this behaviors on the ListViews, so we expand the T as the ListVIew.
and you can use it as such .
<ListView ItemsSource="{Binding RiskViewerService.RiskData}"> <ListView.View> <GridView> </GridView> </ListView.View> <i:Interaction.Behaviors> <b:ListViewBehaviour DataSource="{Binding RiskViewerService.Columns}"></b:ListViewBehaviour> </i:Interaction.Behaviors> </ListView>
相关推荐
gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...
c#-的WPF---MVVM例子,大家共同学习
一个简单的WPF程序
bootstrap-wpf-style-master 样式代码 。。。。。。。。
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手
Prism-Samples-Wpf-master06-10的VS2017版本实现,下载手动重新安装一下nuget包即可,方便大家学习
wpf--ListBox分组-排序 wpf--ListBox分组-排序
我最近打算学习WPF ,在寻找MVVM框架的时候发现了PRISM,在此之前还从一些博客上了解了其它的MVVM框架,比如浅谈WPF中的MVVM框架--MVVMFoundation 中提到的MVVMFoundation,再比如 ViewModel从未如此清爽 - 轻量级...
wpf-wcf-wf整合开发实例--有利于理解wpf-wcf-wf的整合
WPF收集程序
WPF的基本空间历程,使用.net core3.0.1版本
wpf charts源码,D源码,图书:practical-wpf-charts-graphics-master
WPF--Effect特效 对Effect的进行使用的小例子
wpf-datagrid-access DB,可以更新数据到Access数据库
wpf-资源的使用-resource
Wpf-图片放大缩小移动操作 Wpf-图片放大缩小移动操作
WPF-MVVM WPF MVVM模式开发的例子程序
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手
超酷WPF数据可视化,全套源程序,非常适合做数据可视化的程序员使用,WPF,XAML。 找了好久的资源,附有很多很详细的插图,是大数据时代不可缺少的可视化学习数据,仅供大家学习使用请勿用做商业用途。与大家一起...
WPF-MVVM实现CRUD操作