关于SilverLight中的TwoWay绑定. [英] About TwoWay Binding in SilverLight.
问题描述
Hai朋友,
谁能解释一下如何使用TwoWay Binding.例如,
我有2个具有属性Twoway Binding的文本框,我需要将数据从这2个文本框插入到数据库(或)到XmlFile.我知道我们必须使用名为DataContext和Notifychanded属性的类……,但是我不知道如何使用它们.用一些示例代码进行解释将很有帮助.
谢谢.....
Hai friends,
Can anybody explain me how to work with TwoWay Binding. For Example,
I have 2 textboxs with property Twoway Binding, i need to insert the data from those 2 textboxes to my Database (or) to XmlFile. I came to know that we have to use the class called DataContext and Notifychanded property sth..., but i dont know how to use those. It will be helpful to explain with some sample code.
Thanks.....
推荐答案
您需要按照以下步骤操作,
您的绑定类应使用INotifyPropertyChanged接口实现,在该接口中您需要实现相关的属性,如下例所示.
You need to folow below steps,
Your binding class should be implemeted with INotifyPropertyChanged Interface where you need to implement relavant properties, as below example.
public class MyProduct : INotifyPropertyChanged
{
private Dictionary<string, object> _properties = new Dictionary<string, object>();
protected virtual T GetValue<T>(string name)
{
if (this._properties.ContainsKey(name))
return (T)this._properties[name];
return default(T);
}
protected virtual void SetValue<T>(string name, T value)
{
bool set = false;
if (!this._properties.ContainsKey(name))
{
this._properties.Add(name, value);
set = true;
}
else if (!object.Equals(this._properties[name], value))
{
this._properties[name] = value;
set = true;
}
if (set)
{
this.NotifyChange(name);
}
}
protected virtual void NotifyChange(params string[] properties)
{
if (PropertyChanged != null)
{
foreach (string p in properties)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
然后,此类需要使用两种方式的绑定方式与relavnt网格和属性与相对文本框进行绑定.
Then this class need to bind with relavnt gird and property with relavant textbox with two way binding mode.
Mode="TwoWay"
谢谢,
Amit Patel
Thanks,
Amit Patel
Hai Amit M Patel,您的代码很好,但是您可以解释一下如何将文本框中的数据发送到Product Class属性到(SetValue和GetValue),以及如何将数据可以存储到数据库.最好对此有一个清晰的了解,我将用您的示例尝试一下.
Hai Amit M Patel, ur code is fine, but can u pls explain how to send the data from textbox to the Product Class properties to(SetValue & GetValue) and how the data can be stored to database. Better to have some clear understanding about this and i will try my own with ur example.
这篇关于关于SilverLight中的TwoWay绑定.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!