积极答复者
关于sl控件数据绑定的一个问题,是怎么绑定的?

问题
-
最近在读一些sl的源代码,看到这样的代码
<CheckBox Content="AutoCommit" IsChecked="{Binding ElementName=dataForm, Mode=TwoWay, Path=AutoCommit}" Margin="4" /> <CheckBox Content="AutoEdit" IsChecked="{Binding ElementName=dataForm, Mode=TwoWay, Path=AutoEdit}" Margin="4" /> <CheckBox Content="CanUserAddItems" IsChecked="{Binding ElementName=dataForm, Mode=TwoWay, Path=CanUserAddItems}" Margin="4" /> <CheckBox Content="CanUserDeleteItems" IsChecked="{Binding ElementName=dataForm, Mode=TwoWay, Path=CanUserDeleteItems}" Margin="4" /> <CheckBox Content="CommandButtonsVisibility" IsChecked="{Binding ElementName=dataForm, Mode=TwoWay, Path=AutoEdit}" Margin="4" />
第一个问题:里边有个IsChecked属性是否默认选中,它是绑定的一个实体类的哪个字段呀
例如第一行代码,是绑定的path后的那个AutoCommit字段吗?
请看后台代码:
public partial class DataFormSample : UserControl { public DataFormSample() { InitializeComponent(); DataContext = Contact.People; } }
我找到了这个Contact.People实体类,并没发现有“AutoCommit字段”呀。全部
第二个问题:{Binding ElementName=dataForm, Mode=TwoWay, Path=AutoCommit}
麻烦解释下大括号里的每个代码的作用,Mode=TwoWay是双向绑定,我明白这个。其他两个就不知道了。
第三个问题:这样的绑定方式,为什么以前在asp.net,html的编程里没涉及到呢?
我觉得直接后台c#里给控件赋值不就可以了吗,为什么还需要这样的绑定呢,不过绑定的方式确实很简便。
谢谢
生活因软件而改变
答案
-
<CheckBox Content="AutoCommit" IsChecked="{Binding ElementName=dataForm, Mode=TwoWay, Path=AutoCommit}" Margin="4" />
重新解释一遍,上面这句的意思是,你的这个CheckBox控件就跟你dataForm控件的AutoCommit的值相关了。
相当于你的数据源对应到dataForm这个控件中
而上面这个ChechBox只关心你detaForm控件的AutoCommit,数据源没有并不代表dataForm控件没有这个属性,再看看- 已标记为答案 woodynet 2009年5月8日 6:57
全部回复
-
Silverlight 2.0 数据绑定:
Binding - 将绑定目标对象的属性与数据源联接起来
Source - 绑定的数据源
Mode - 绑定的数据流的方向 [System.Windows.Data.BindingMode枚举]
BindingMode.OneTime - 一次绑定。创建绑定时一次性地更新绑定目标对象的属性
BindingMode.OneWay - 单向绑定(默认值)。数据源的改变会自动通知到绑定目标对象的属性
BindingMode.TwoWay - 双向绑定。数据源或绑定目标对象的属性的值发生改变时会互相通知。显然,做数据验证的话一定要是双向绑定
这里例子很详细:http://www.cnblogs.com/webabcd/archive/2009/01/05/1337190.htmlMy blog: http://blog.csdn.net/dotfun http://dotfun.cnblogs.com
My contact: QQ:372900288 E-mail:372900288@qq.com msn:sellnet007@hotmail.com
- 已建议为答案 风云-魏永超Moderator 2009年5月7日 9:55
-
谢谢楼上两位,可是问题我还是没搞清楚呀。
一,“1.绑定到People类型AutoCommit属性,请再查看People类”,我看过people类了(people只是个Contact类的静态属性,请看下帖代码),确实没有“AutoCommit”属性呀。
我一会贴过来这个类的代码,麻烦你们看看。
二,:{Binding ElementName=dataForm, Path=AutoCommit}
请问ElementName是什么意思?要绑定的类的类名?
请问Path是什么意思?要绑定的属性名,估计这个我猜对了?
谢谢呀。
视别人的帮助为恩赐~- 已编辑 woodynet 2009年5月7日 10:13
-
using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace System.Windows.Controls.Samples { public class Contact : INotifyPropertyChanged, IEditableObject { public Contact(); [Display(Name = "City", Description = "City of residence", GroupName = "Address")] [Required] public string City { get; set; } [Display(Name = "Email", Description = "An e-mail address of the form <name>@<domain>, such as john@johndoe.com")] [RegularExpression(@"^([a-zA-Z0-9\!\%\$\%\*\/\?\|\^\{\}\`\~\&\'\+\-\=_]\.?)*[a-zA-Z0-9\!\%\$\%\*\/\?\|\^\{\}\`\~\&\'\+\-\=_]@((([a-zA-Z0-9\!\%\$\%\*\/\?\|\^\{\}\`\~\&\'\+\-\=_]\.?)*[a-zA-Z0-9\!\%\$\%\*\/\?\|\^\{\}\`\~\&\'\+\-\=_])|(\[\d+\.\d+\.\d+\.\d+\]))$", ErrorMessage = "Not a valid e-mail address.")] public string Email { get; set; } [Required] [Display(Name = "First Name", GroupName = "Name")] public string FirstName { get; set; } [Display(Name = "Business Address", Description = "Indicates that the address is a business address.")] public bool IsBusinessAddress { get; set; } public static Contact JohnDoe { get; } [Required] [Display(Name = "Last Name", GroupName = "Name")] public string LastName { get; set; } public static IEnumerable<Contact> People { get; } [Display(Name = "Phone", Description = "Phone number of the form (###) ###-####")] [RegularExpression(@"^\(\d\d\d\) \d\d\d\-\d\d\d\d$", ErrorMessage = "Not a valid phone number. Please enter a phone number that matches the format (###) ###-####")] public string Phone { get; set; } [Required] [Display(Name = "State", Description = "State abbreviation", GroupName = "Address")] [RegularExpression("^[a-zA-Z][a-zA-Z]$", ErrorMessage = "Not a valid state abbreviation (e.g. CA or TN)")] public string State { get; set; } [Display(Name = "Street Address", GroupName = "Address")] [Required] public string Street1 { get; set; } [Display(Name = "Secondary Street Address", Description = "Additional street address information, such as apartment number or P.O. Box", GroupName = "Address")] public string Street2 { get; set; } [Display(Name = "Zip", Description = "Five-digit zip code", GroupName = "Address")] [RegularExpression(@"^\d\d\d\d\d$", ErrorMessage = "Zip codes must be 5-digit numbers.")] [Required] public int Zip { get; set; } public event PropertyChangedEventHandler PropertyChanged; public void BeginEdit(); public void CancelEdit(); public void EndEdit(); protected virtual void OnPropertyChanged(string propName); } }
视别人的帮助为恩赐~ -
/// <summary> /// Gets a stock group of contacts with valid contact information. /// </summary> public static IEnumerable<Contact> People { get { return new ObservableCollection<Contact> { new Contact { FirstName = "Michiel", LastName = "Wories", Phone = "(555) 555-8584", Street1 = "199 Anywhere Street", City = "AnotherTown", State = "NY", Zip = 39495 }, new Contact { FirstName = "John", LastName = "Yokim", Phone = "(555) 555-4843", Street1 = "3241 Someplace Street", City = "Anytown", State = "NY", Zip = 93258 }, new Contact { FirstName = "Yanlai", LastName = "Guo", Phone = "(555) 555-4999", Street1 = "2241 Somewhere Street", City = "Anytown", State = "WA", Zip = 18444 } }; } }
视别人的帮助为恩赐~ -
<CheckBox Content="AutoCommit" IsChecked="{Binding ElementName=dataForm, Mode=TwoWay, Path=AutoCommit}" Margin="4" />
重新解释一遍,上面这句的意思是,你的这个CheckBox控件就跟你dataForm控件的AutoCommit的值相关了。
相当于你的数据源对应到dataForm这个控件中
而上面这个ChechBox只关心你detaForm控件的AutoCommit,数据源没有并不代表dataForm控件没有这个属性,再看看- 已标记为答案 woodynet 2009年5月8日 6:57
-
熊哥,你看,咋就找不到AutoCommit属性呀。谢谢呀
视别人的帮助为恩赐~
<UserControl x:Class="SilverlightApplication2.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <Canvas x:Name="LayoutRoot" Background="White" Width="400" Height="300" > <Rectangle x:Name="Kare" Width="100" Height="100" Fill="Red" Canvas.Top="50" Canvas.Left="140"> <Rectangle.Projection> <PlaneProjection x:Name="Kare3d" CenterOfRotationX="0.5" CenterOfRotationY="0.5" CenterOfRotationZ="0"/> </Rectangle.Projection> </Rectangle> <Slider Minimum="0" Maximum="360" Width="180" Value="{Binding ElementName=Kare3d, Mode=TwoWay, Path=RotationX}" Canvas.Left="20" Canvas.Top="200"/> <Slider Minimum="0" Maximum="360" Width="180" Value="{Binding ElementName=Kare3d, Mode=TwoWay, Path=RotationY}" Canvas.Left="20" Canvas.Top="230" /> <Slider Minimum="0" Maximum="360" Width="180" Value="{Binding ElementName=Kare3d, Mode=TwoWay, Path=RotationZ}" Canvas.Left="20" Canvas.Top="260"/> <Slider Minimum="0" Maximum="1" Width="180" Value="{Binding ElementName=Kare3d, Mode=TwoWay, Path=CenterOfRotationX}" Canvas.Top="200" Canvas.Left="210"/> <Slider Minimum="0" Maximum="1" Width="180" Value="{Binding ElementName=Kare3d, Mode=TwoWay, Path=CenterOfRotationY}" Canvas.Top="230" Canvas.Left="210"/> <Slider Minimum="0" Maximum="1" Width="180" Value="{Binding ElementName=Kare3d, Mode=TwoWay, Path=CenterOfRotationZ}" Canvas.Top="260" Canvas.Left="210"/> <TextBlock Canvas.Top="180" Canvas.Left="20" Width="87" Height="16" FontWeight="Bold" Text="Rotation"/> <TextBlock Canvas.Top="180" Canvas.Left="210" Height="16" Width="118" Text="CenterOfRotation" FontWeight="Bold"/> <TextBlock Canvas.Top="200" Canvas.Left="4" Height="16" Width="8" Text="X" FontWeight="Bold"/> <TextBlock Canvas.Top="230" Canvas.Left="4" Height="16" Width="8" Text="Y" FontWeight="Bold"/> <TextBlock Canvas.Top="260" Canvas.Left="4" Height="16" Width="8" Text="Z" FontWeight="Bold"/> </Canvas> </UserControl>
看上面这个例子可能更好理解,几个Silder的值都绑定在Kare3d这个控件上,RotationX这个值Kare3d并没有显示声明出来,但是PlaneProjection是可以取得这个值的,标识x坐标轴旋转的角度