[英]Problem with data binding in C#
我试图更好地理解数据绑定在.net中的工作原理。 我正在查看这篇文章,我想出了这段代码:
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
时调用changeMyTextButton_Click
)。 我错过了什么吗?
编辑:更简单:我想编写一个带有属性string MyText
的用户控件,然后用户可以绑定到其他东西,就像将TextBox
的Text
属性绑定到其他东西一样。 所以我不想将控件的属性绑定到对象,我想用一个属性编写一个控件,然后用户可以绑定到一个对象。
好吧我想出来以防万一有人遇到同样的问题。 我必须创建一个名为MyTextChanged
的事件处理程序,让Binding
知道MyText
正在改变,并将Binding
的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"));
}
}
也许这会帮助你理解何时执行Parse事件。
要查看binding_Parse工作,请查看此示例:
public partial class Form1 : Form
{
public MyClass myClass { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myClass = new MyClass();
myClass.AddStuff("uno", "UNO");
Binding b = new Binding("Text", myClass, "Dic");
b.Parse += new ConvertEventHandler(b_Parse);
b.Format += new ConvertEventHandler(b_Format);
textBox1.DataBindings.Add(b);
}
void b_Format(object sender, ConvertEventArgs e)
{
e.Value = (e.Value as Dictionary<string, string>)["uno"].ToString();
textBox1.Text = e.Value.ToString();
}
void b_Parse(object sender, ConvertEventArgs e)
{
MessageBox.Show("This is executed when you lost focus\n\nI'm parsing your entered text: " + e.Value);
}
}
你只需要调试。 单击按钮,然后注意首先调用Format事件。 然后,如果您手动更改textbox1.Text值,当您再次单击该按钮时,将会失去焦点,然后将执行Parse事件。
要创建自定义控件,请检查此站点 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.