Datagrid加载和提交时候自动出现BusyIndicator并绑定

BusyIndicator和DataGrid搭配使用的效果就是当Datagrid加载数据或者提交的时候自动灰掉并出现“Please wait...”进度条,这是一个非常普通的需求,因为从服务器端加载和提交数据谁也不知道需要耗时多久,很多其他的框架比如ExtJS都有很好的支持。在Silverlight中也是很方面的,就是将需要等待载入的控件(这里是DataGrid),作为BusyIndicator 的子控件即可。需要注意的是BusyIndicator自动绑定ViewModel的IsBusy属性怎么做?ViewModel的IsBusy属性如何在加载数据和更新数据的时候进行更新?貌似一个小功能,但要做的完美才行。

前台Xaml:将需要等待载入的控件(这里是DataGrid),作为BusyIndicator 的子控件,IsBusy绑定ViewModel的属性。

复制代码
  
  
< UserControl x:Class ="TestUserControl" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d ="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable ="d" d:Height ="Auto" d:Width ="Auto" xmlns:sdk ="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:toolkit ="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" > < toolkit:HeaderedContentControl Name ="headeredContentControl1" Width ="Auto" > < toolkit:BusyIndicator Name ="busyIndicator1" IsBusy =" {Binding IsBusy} " DisplayAfter ="0" BusyContent ="Transferring data..." > < sdk:DataGrid ItemSource =" {Binding EntitiesList} " AutoGenerateColumns ="False" GridLinesVisibility ="None" Width ="Auto" HorizontalAlignment ="Stretch" VerticalAlignment ="Stretch" BorderThickness ="0,0,0,0" SelectionMode ="Single" Name ="dgvRecentUpdated" > < sdk:DataGrid.Columns > < sdk:DataGridTextColumn Header ="EntityID" Binding =" {Binding EntityID} " Visibility ="Collapsed" /> < sdk:DataGridTextColumn Header ="Entity name" Binding =" {Binding EntityName} " Width ="120" /> < sdk:DataGridTextColumn Header ="Entity desc" Binding =" {Binding EntityDesc} " Width ="120" /> </ sdk:DataGrid.Columns > </ sdk:DataGrid > </ toolkit:BusyIndicator > </ toolkit:HeaderedContentControl > </ UserControl >
复制代码

ViewModel的IsBusy属性设置:当DomainEntity加载和提交的时候自动更新状态

复制代码
  
  
1 public Boolean IsBusy 2 { 3 get 4 { 5 return this ._isBusy; 6 } 7 8 private set 9 { 10 if ( this ._isBusy != value) 11 { 12 this ._isBusy = value; 13 this .RaisePropertyChanged( " IsBusy " ); // 需ViewModel已经实现了INotifyPropertyChanged接口 14 } 15 } 16 } 17 private Boolean _isBusy = false ; 18 19 // WCF Ria Service的DomainContxt 20 21 private MyDomainContext _ctx; 22 23 protected MyDomainContext Context 24 { 25 get 26 { 27 if (_ctx == null ) 28 { 29 _ctx = new MyDomainContext(); 30 _ctx.PropertyChanged += new PropertyChangedEventHandler(_ctx_PropertyChanged); 31 } 32 33 return _ctx; 34 } 35 } 36 37 private void _ctx_PropertyChanged( object sender, System.ComponentModel.PropertyChangedEventArgs e) 38 { 39 switch (e.PropertyName) 40 { 41 // case "HasChanges": 42 // this.HasChanges = _ctx.HasChanges; 43 // break; 44 case " IsLoading " : 45 this .IsBusy = _ctx.IsLoading; 46 break ; 47 case " IsSubmitting " : 48 this .IsBusy = _ctx.IsSubmitting; 49 break ; 50 } 51 } 52 53 // 从WCF Ria Service取数据: Context.Load<T>....
复制代码

如果你的应用非常简单直接用DomainDataSource,那么就直接把BusyIndicator的IsBusy属性绑定到你的DomainDataSource,会自动改变状态:

  
  
< sdk:BusyIndicator IsBusy =" {Binding IsBusy, ElementName=targetDomainDataSource} " ... />

自定义BusyIndicator的样式

现在Silverlight的BusyIndicator样式很难看,你可以自定义,比如做一个Firefox的圆形样式:

复制代码
  
  
1 < UserControl x:Class ="SilverlightApplication1.CustomBusyIndicator" 2 xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d ="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 mc:Ignorable ="d" 7 xmlns:sys ="clr-namespace:System;assembly=mscorlib" 8 d:Height ="Auto" d:Width ="Auto" > 9 < UserControl.Resources > 10 < sys:String x:Key ="data" > M 0,0 L-2,0 L -2,-0 L0,-10 L 2,-10 L 2,0 Z </ sys:String > 11 < Storyboard x:Name ="Storyboard1" > 12 < DoubleAnimation BeginTime ="0:0:0" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p1" /> 13 < DoubleAnimation BeginTime ="0:0:0.8" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p2" /> 14 < DoubleAnimation BeginTime ="0:0:0.16" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p3" /> 15 < DoubleAnimation BeginTime ="0:0:0.24" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p4" /> 16 < DoubleAnimation BeginTime ="0:0:0.32" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p5" /> 17 < DoubleAnimation BeginTime ="0:0:0.40" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p6" /> 18 < DoubleAnimation BeginTime ="0:0:0.48" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p7" /> 19 < DoubleAnimation BeginTime ="0:0:0.56" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p8" /> 20 < DoubleAnimation BeginTime ="0:0:0.64" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p9" /> 21 < DoubleAnimation BeginTime ="0:0:0.72" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p10" /> 22 < DoubleAnimation BeginTime ="0:0:0.80" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p11" /> 23 < DoubleAnimation BeginTime ="0:0:0.88" Duration ="0:0:1" RepeatBehavior ="Forever" From ="1" To ="0.2" Storyboard.TargetProperty ="(UIElement.Opacity)" Storyboard.TargetName ="p12" /> 24 </ Storyboard > 25 </ UserControl.Resources > 26 < Grid x:Name ="LayoutRoot" Background ="White" Margin ="100" > 27 < Path x:Name ="p1" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 28 < Path.RenderTransform > 29 < TransformGroup > 30 < TranslateTransform Y ="-8" /> 31 < RotateTransform Angle ="0" /> 32 </ TransformGroup > 33 </ Path.RenderTransform > 34 </ Path > 35 < Path x:Name ="p2" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 36 < Path.RenderTransform > 37 < TransformGroup > 38 < TranslateTransform Y ="-8" /> 39 < RotateTransform Angle ="30" /> 40 </ TransformGroup > 41 </ Path.RenderTransform > 42 </ Path > 43 < Path x:Name ="p3" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 44 < Path.RenderTransform > 45 < TransformGroup > 46 < TranslateTransform Y ="-8" /> 47 < RotateTransform Angle ="60" /> 48 </ TransformGroup > 49 </ Path.RenderTransform > 50 </ Path > 51 < Path x:Name ="p4" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 52 < Path.RenderTransform > 53 < TransformGroup > 54 < TranslateTransform Y ="-8" /> 55 < RotateTransform Angle ="90" /> 56 </ TransformGroup > 57 </ Path.RenderTransform > 58 </ Path > 59 < Path x:Name ="p5" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 60 < Path.RenderTransform > 61 < TransformGroup > 62 < TranslateTransform Y ="-8" /> 63 < RotateTransform Angle ="120" /> 64 </ TransformGroup > 65 </ Path.RenderTransform > 66 </ Path > 67 < Path x:Name ="p6" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 68 < Path.RenderTransform > 69 < TransformGroup > 70 < TranslateTransform Y ="-8" /> 71 < RotateTransform Angle ="150" /> 72 </ TransformGroup > 73 </ Path.RenderTransform > 74 </ Path > 75 < Path x:Name ="p7" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 76 < Path.RenderTransform > 77 < TransformGroup > 78 < TranslateTransform Y ="-8" /> 79 < RotateTransform Angle ="180" /> 80 </ TransformGroup > 81 </ Path.RenderTransform > 82 </ Path > 83 < Path x:Name ="p8" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 84 < Path.RenderTransform > 85 < TransformGroup > 86 < TranslateTransform Y ="-8" /> 87 < RotateTransform Angle ="210" /> 88 </ TransformGroup > 89 </ Path.RenderTransform > 90 </ Path > 91 < Path x:Name ="p9" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Opacity ="0.2" Fill ="Black" > 92 < Path.RenderTransform > 93 < TransformGroup > 94 < TranslateTransform Y ="-8" /> 95 < RotateTransform Angle ="240" /> 96 </ TransformGroup > 97 </ Path.RenderTransform > 98 </ Path > 99 < Path x:Name ="p10" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 100 < Path.RenderTransform > 101 < TransformGroup > 102 < TranslateTransform Y ="-8" /> 103 < RotateTransform Angle ="270" /> 104 </ TransformGroup > 105 </ Path.RenderTransform > 106 </ Path > 107 < Path x:Name ="p11" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 108 < Path.RenderTransform > 109 < TransformGroup > 110 < TranslateTransform Y ="-8" /> 111 < RotateTransform Angle ="300" /> 112 </ TransformGroup > 113 </ Path.RenderTransform > 114 </ Path > 115 < Path x:Name ="p12" Data =" {Binding Source={StaticResource data}} " Stroke ="Gray" Fill ="Black" Opacity ="0.2" > 116 < Path.RenderTransform > 117 < TransformGroup > 118 < TranslateTransform Y ="-8" /> 119 < RotateTransform Angle ="330" /> 120 </ TransformGroup > 121 </ Path.RenderTransform > 122 </ Path > 123 </ Grid > 124 125 </ UserControl >
复制代码

一个小小的BusyIndicator居然能扯出来这么多东西!其实写程序就是和建筑工人/泥水匠差不多,重在细节,如果一个砖头没有砌好,大厦就有可能倾斜。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值