我试图更好地了解.net中的数据绑定如何工作.我正在查看this条文章,然后想到了以下代码:
public partial class Form1 : Form//, INotifyPropertyChanged
{
public event PropertyChangedEventHandler MyTextChanged;
[System.ComponentModel.Bindable(true)]
public string MyText
{
get { return textBox1.Text; }
set
{
textBox1.Text = value;
if (MyTextChanged != null)
MyTextChanged(this, new PropertyChangedEventArgs("MyText"));
}
}
MyClass myClass { get; set; }
public Form1()
{
InitializeComponent();
myClass = new MyClass();
Binding binding = new Binding("MyText", myClass, "Dic");
binding.Parse += new ConvertEventHandler(binding_Parse);
binding.Format += new ConvertEventHandler(binding_Format);
DataBindings.Add(binding);
myClass.AddStuff("uno", "UNO");
}
void OnMyTextChanged(PropertyChangedEventArgs e)
{
if (MyTextChanged != null) MyTextChanged(this, e);
}
void binding_Format(object sender, ConvertEventArgs e)
{
if (e.Value is Dictionary<string, string>)
{
Dictionary<string, string> source = (Dictionary<string, string>)e.Value;
e.Value = source.Count.ToString();
}
}
void binding_Parse(object sender, ConvertEventArgs e)
{
MessageBox.Show(e.DesiredType.ToString());
}
private void changemyClassButton_Click(object sender, EventArgs e)
{
myClass.AddStuff(myClass.Dic.Count.ToString(), "'" + myClass.Dic.Count.ToString() + "'");
}
private void changeMyTextButton_Click(object sender, EventArgs e)
{
MyText = "1234";
}
}
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Dictionary<string, string> Dic { get; set; }
public MyClass()
{
Dic = new Dictionary<string, string>();
}
public void AddStuff(string key, string value)
{
Dic.Add(key, value);
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Dic"));
}
}
我试图将MyText绑定到myClass.问题在于,永远不会调用函数binding_Parse.我知道我可能可以直接将textBox1.Text绑定到myClass,或者可能有成千上万种其他方法可以完成我想做的事情,但这只是一种实践;我试图了解更好的数据绑定.因此,我想将自定义对象绑定到自定义属性,以便可以从头到尾看到该过程.自定义对象是myClass,而自定义属性是MyText.我已经尝试了各种变体,例如实现INotifyPropertyChanged,但是我无法调用binding_Parse(我希望在调用changeMyTextButton_Click时会调用它).我想念什么吗?
编辑:
简而言之:我想编写一个带有属性字符串MyText的用户控件,然后用户可以将其绑定到其他对象,就像将TextBox的Text属性绑定到其他对象一样.因此,我不想将控件的属性绑定到对象,而是想编写具有属性的控件,然后用户可以将其绑定到对象.
解决方法:
好吧,我想通了,以防万一有人遇到同样的问题.我必须创建一个名为MyTextChanged的事件处理程序,以使Binding知道MyText正在更改,并将Bindings DataSourceUpdateMode属性设置为OnPropertyChanged.使用这个简单的原理,我可以将屏幕上的像素绑定到宇宙的其余部分:).这是代码:
public partial class Form1 : Form
{
public event EventHandler MyTextChanged;
[Bindable(true)]
public string MyText
{
get { return textBox1.Text; }
set
{
if (textBox1.Text != value)
{
textBox1.Text = value;
OnMyTextChanged();
}
}
}
MyClass myClass { get; set; }
public Form1()
{
InitializeComponent();
myClass = new MyClass();
Binding binding = new Binding("MyText", myClass, "Dic");
binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
binding.Parse += new ConvertEventHandler(binding_Parse);
binding.Format += new ConvertEventHandler(binding_Format);
DataBindings.Add(binding);
myClass.AddStuff("uno", "UNO");
}
void OnMyTextChanged()
{
if (MyTextChanged != null) MyTextChanged(this, EventArgs.Empty);
}
void binding_Format(object sender, ConvertEventArgs e)
{
if (e.Value is Dictionary<string, string>)
{
Dictionary<string, string> source = (Dictionary<string, string>)e.Value;
e.Value = source.Count.ToString();
}
}
void binding_Parse(object sender, ConvertEventArgs e)
{
MessageBox.Show(e.DesiredType.ToString());
}
private void changemyClassButton_Click(object sender, EventArgs e)
{
myClass.AddStuff(myClass.Dic.Count.ToString(), "'" + myClass.Dic.Count.ToString() + "'");
}
private void changeMyTextButton_Click(object sender, EventArgs e)
{
MyText = "1234";
}
}
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Dictionary<string, string> Dic { get; set; }
public MyClass()
{
Dic = new Dictionary<string, string>();
}
public void AddStuff(string key, string value)
{
Dic.Add(key, value);
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Dic"));
}
}