建筑工程教育网官方网站,视频制作教程自学,扶风做企业网站,免费注册qq号网站本文将详细讲述Silverlight中Binding#xff0c;包括Binding的属性和用法#xff0c;Binding的数据流向。 Binding:一个完整的Binding过程是让源对象中的某个属性值通过一定流向规则进行转换和验证之后绑定到目标对象的某个属性上面。这个源对象由ElementName指定#xff0c… 本文将详细讲述Silverlight中Binding包括Binding的属性和用法Binding的数据流向。 Binding:一个完整的Binding过程是让源对象中的某个属性值通过一定流向规则进行转换和验证之后绑定到目标对象的某个属性上面。这个源对象由ElementName指定源对象的属性由Path指定流向规则由Mode指定转换由Converter指定验证由ValidatesOnDataErrors等指定。 首先我们来看Binding的属性如下 ElementName:指定源对象的名称 Path:指定需要绑定的源对象的属性名称 Mode:指定Binding的数据流向规则 Converter:指定源对象的属性需要经过用户自定义的转换 其次我们来看看Binding的数据流向Mode分为以下几种 OneTime:源对象的属性只有在第一次的时候绑定到目标对象以后源对象属性值变化时目标对象值不变 OneWay:源对象的属性值变化的时候目标对象值也跟着相应变化而目标对象值变化时源对象属性值不变 TwoWay:源对象的属性值变化的时候目标对象值也跟着相应变化目标对象值变化时源对象属性值也跟着变 下面我们通过以下实例源码来看看Binding的简单应用和转换注意Mode为TwoWay的时候目标对象更新时需要转移焦点(LostFocus)才触发更新源对象。例如本文实例中需要点击到另外的TextBox才更新源。 Xaml UserControl x:ClassSLBinding.MainPage xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:localclr-namespace:SLBinding mc:Ignorabled d:DesignHeight600 d:DesignWidth800 xmlns:sdkhttp://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk UserControl.Resources local:ImageConverter x:KeyImageCoverter/ /UserControl.Resources Grid x:NameLayoutRoot BackgroundWhite !--One Time-- StackPanel OrientationHorizontal sdk:Label Height28 HorizontalAlignmentLeft Margin130,56,0,0 Namelabel1 VerticalAlignmentTop Width120 ContentOne Time: / TextBox Height23 HorizontalAlignmentLeft Margin20,56,0,0 NametbOneTimeSource VerticalAlignmentTop Width120 Text初次绑定 / TextBox Height23 HorizontalAlignmentLeft Margin20,56,0,0 NametbOneTimeTarget VerticalAlignmentTop Width120 Text{Binding ElementNametbOneTimeSource, PathText, ModeOneTime}/ /StackPanel !--One Way-- StackPanel OrientationHorizontal sdk:Label Height28 HorizontalAlignmentLeft Margin130,100,0,0 Namelabel2 VerticalAlignmentTop Width120 ContentOne Way: / TextBox Height23 HorizontalAlignmentLeft Margin20,100,0,0 NametbOneWaySource VerticalAlignmentTop Width120 Text单向绑定 / TextBox Height23 HorizontalAlignmentLeft Margin20,100,0,0 NametbOneWayTarget VerticalAlignmentTop Width120 Text{Binding ElementNametbOneWaySource, PathText, ModeOneWay}/ /StackPanel !--Two Way-- StackPanel OrientationHorizontal sdk:Label Height28 HorizontalAlignmentLeft Margin130,150,0,0 Namelabel3 VerticalAlignmentTop Width120 ContentOne Time: / TextBox Height23 HorizontalAlignmentLeft Margin20,150,0,0 NametbTwoWaySource VerticalAlignmentTop Width120 Text双向绑定 / TextBox Height23 HorizontalAlignmentLeft Margin20,150,0,0 NametbTwoWayTarget VerticalAlignmentTop Width120 Text{Binding ElementNametbTwoWaySource, PathText, ModeTwoWay}/ /StackPanel !--Converter-- StackPanel OrientationHorizontal sdk:Label Height28 HorizontalAlignmentLeft Margin130,220,0,0 Namelabel5 VerticalAlignmentTop Content下面将网络图片地址使用Converter自动绑定转换为图片显示出来 / /StackPanel StackPanel OrientationHorizontal sdk:Label Height28 HorizontalAlignmentLeft Margin130,250,0,0 Namelabel4 VerticalAlignmentTop Width120 ContentConverter: / TextBox Height23 HorizontalAlignmentLeft Margin20,250,0,0 NametbConverter VerticalAlignmentTop Texthttp://sc.admin5.com/uploads/allimg/100211/105R33342-7.png / Image NameimgCity Width60 Height60 Source{Binding ElementNametbConverter,PathText, ModeTwoWay, Converter{StaticResource ImageCoverter}}/Image /StackPanel /Grid/UserControl ImageConverter.cs public class ImageConverter : IValueConverter {//在载入数据的时候将数据转换为图片类型 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {try { Uri uri new Uri((string)value, UriKind.RelativeOrAbsolute); BitmapImage img new BitmapImage(uri);return img; }catch {return new BitmapImage(); } }//在页面上操作的时候将图片类型转换为数据这里只有再TwoWay的时候才有用 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { BitmapImage img value as BitmapImage;return img.UriSource.AbsoluteUri; } } 下面我们来看看本实例运行效果如下图如需源码请点击 SLBinding.zip 下载