[英]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.