|
Hello:
I've another question regarding BindingContexts.
I have a TabControl with two TabPages. I am also working with a List<Person>. For simplicity, each TabPage has two controls: a ListBox and a TextBox. Now I want the pages to reference the same List<Person>, but have different indexers.
As such, I do the following...
this.tabPage1.BindingContext = new BindingContext(); this.tabPage2.BindingContext = new BindingContext();
So we can see the pages will have two different BindingContexts. Now, I bind the data...
this.listBox1.DataSource = lstPersons; this.listBox2.DataSource = lstPersons;
this.textBox1.DataBindings.Add("Text", lstPersons, "Name"); this.textBox2.DataBindings.Add("Text", lstPersons, "Name");
Only textBox1 changes its text when the user selects a person from listBox1. textBox2's text doesn't change when I select a person from listBox2. So, what's going on?
If I don't give each TabPage its own BindingContext, then if a user selects a person in listBox2, then textBox2 reflects the change; unfortunately, because they're sharing the same BindingContext, the text for textBox1 in tabPage1 also changes. How can I make so both TabPages can reference the same List<Person>, but have different indices to that data?
Thank you.
Trecius | | Trecius Tuesday, May 12, 2009 3:28 AM | Hi Trecius, Based on my test with your code, the TextBox in tabpage2 will not show the Text. I've figured out a way to resolve this. This can be solved simply by removing "this.tabPage2.BindingContext = new BindingContext()". But I just haven't figured out why by now. Please let me know if it doesn't work on your side. Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.- Marked As Answer byBruce.ZhouMSFT, ModeratorSaturday, May 16, 2009 2:28 AM
-
| | Bruce.Zhou Thursday, May 14, 2009 10:52 AM | Hi Trecius, Based on my test with your code, the TextBox in tabpage2 will not show the Text. I've figured out a way to resolve this. This can be solved simply by removing "this.tabPage2.BindingContext = new BindingContext()". But I just haven't figured out why by now. Please let me know if it doesn't work on your side. Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.- Marked As Answer byBruce.ZhouMSFT, ModeratorSaturday, May 16, 2009 2:28 AM
-
| | Bruce.Zhou Thursday, May 14, 2009 10:52 AM | Mr. Zhou:
Thank you for your response. I've decided to take an alternate route. I, now, just create a new BindingSource, and use that as the DataBinding. Thank you, though, for creating a test bed and testing it. I'm sorry you had to go through so much.
Trecius | | Trecius Friday, May 15, 2009 3:05 PM | You are welcome. But if you I have time, you can have a try with my suggestion. Best regards, Bruce Zhou Please mark the replies as answers if they help and unmark if they don't. | | Bruce.Zhou Saturday, May 16, 2009 2:28 AM |
|