微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 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 ="busyIndicator1" IsBusy =" {Binding IsBusy} " displayAfter ="0" BusyContent ="Transferring data..." sdk:DataGrid ItemSource {Binding EntitiesList} AutoGenerateColumns ="False" GridLinesVisibility ="None" HorizontalAlignment ="Stretch" VerticalAlignment BorderThickness ="0,0" SelectionMode ="Single" Name ="dgvRecentUpdated" sdk:DataGrid.Columns sdk:DataGridTextColumn Header ="EntityID" Binding {Binding EntityID} Visibility ="Collapsed" /> ="Entity name" {Binding EntityName} ="120" ="Entity desc" {Binding EntityDesc} ="120" </ 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 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 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 IsSubmitting 48 this .IsBusy = _ctx.IsSubmitting; 49 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的圆形样式:

="SilverlightApplication1.CustomBusyIndicator" 2 3 4 5 6 7 xmlns:sys ="clr-namespace:System;assembly=mscorlib" 8 9 UserControl.Resources 10 sys:String x:Key ="data" > M 0,0 L-2,0 L -2,-0 L0,-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 ="0:0:0.8" ="p2" 14 ="0:0:0.16" ="p3" 15 ="0:0:0.24" ="p4" 16 ="0:0:0.32" ="p5" 17 ="0:0:0.40" ="p6" 18 ="0:0:0.48" ="p7" 19 ="0:0:0.56" ="p8" 20 ="0:0:0.64" ="p9" 21 ="0:0:0.72" ="p10" 22 ="0:0:0.80" ="p11" 23 ="0:0:0.88" ="p12" 24 Storyboard 25 26 Grid ="LayoutRoot" Background ="White" Margin ="100" 27 Path ="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 33 34 Path 35 ="p2" 36 37 38 39 ="30" 40 41 42 43 44 45 46 47 ="60" 48 49 50 51 52 53 54 55 ="90" 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 ="150" 72 73 74 75 76 77 78 79 ="180" 80 81 82 83 84 85 86 87 ="210" 88 89 90 91 ="Black" 92 93 94 95 ="240" 96 97 98 99 100 101 102 103 ="270" 104 105 106 107 108 109 110 111 ="300" 112 113 114 115 116 117 118 119 ="330" 120 121 122 123 Grid 124 125

复制代码

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐