HighTechTalks DotNet Forums  

Simple Data Binding Questions

Dotnet Framework (WinForms Controls) microsoft.public.dotnet.framework.windowsforms.controls


Discuss Simple Data Binding Questions in the Dotnet Framework (WinForms Controls) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Paul Schwann
 
Posts: n/a

Default Simple Data Binding Questions - 12-18-2009 , 01:49 AM






Hi there,

I have a couple of questions regarding simple data binding. But some
coordinates first:
..NET 2.0, Visual Studio 2005 SP1, Windows XP

Given the following scenario:

A form has 3 components: A textbox, a label and a button. textbox and
label are bound with
data binding to the properties of a model (say Text1 and Text2). The
model looks like this:

class Model : INotifyPropertyChanged {
private String text1 = "foo", text2 = "bar";
public event PropertyChangedEventHandler PropertyChanged;

public String Text1 {
get { return text1; }
set {
if (!String.Equals(text1, value)) {
text1 = value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Text1"));
}
}
}
}

public String Text2 {
get { return text2; }
set {
if (!String.Equals(text2, value)) {
text2 = value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Text2"));
}
}
}
}
}

The form and data binding look like that:

class Form1 : Form {
private Model m;
public Form1() {
m = new Model();
TextBox textbox = new TextBox();
textbox.Location = new Point(0, 0);
Label label = new Label();
label.Location = new Point(0, 30);
this.Controls.Add(textbox);
this.Controls.Add(label);
// button just to be able to move the focus away from the text box.
Button b = new Button();
b.Text = "Button";
b.Location = new Point(0, 50);
this.Controls.Add(b);

textbox.DataBindings.Add(
"Text", m, "Text1", false, DataSourceUpdateMode.OnPropertyChanged);
label.DataBindings.Add(
"Text", m, "Text2", false, DataSourceUpdateMode.OnPropertyChanged);
}
}

-------------------

Here is my first question:

1) If I modify the text in the textbox, the property Text1 of the
model gets updated. But the property binding mechanism also asks for
the property value Text2 which hasn't changed. Why is that and how can
I surpress this?
I have the impression that the data binding ensures that all values of
one model are refreshed (in the view) if one of them changes. Is that
true?

A potential problem might be the following: Imagine that the label is
updated by some thread every 500ms (some status or so). Than all
properties bound to the form (and there might be dozens!) are also
refreshed (although they haven't changed). I feel that this could be
lead to performance issues.

--------------------

Here is the second question:

Assume I want the textbox to contain only 4 hexadecimal digits. Thus,
I modify the data binding for the textbox as shown below:

textbox.DataBindings.Add(
"Text", m, "Text1", true, DataSourceUpdateMode.OnValidation,
"", "X4", CultureInfo.CurrentUICulture);

My expectation is (and that's also what I read in the msdn
documentation) that the focus of the textbox field can not be removed
until a valid value is entered. But I am wrong. What I see is that the
setter for Text1 is indeed called only if I press TAB key while in the
textbox. But the value can be anything. How do I do it right? What is
the formatting information good for anyway?

----------------------

And here is the third and final question (actually a combination of
the first two):

Assume that label is updated every second from the main program, some
status scan thread or whatever. Trying this shows the following,
completely broken behavior: While I am editing textbox, the text is
reset to its original value each time the label is updated.
My explanation:
The textbox data binding is set to OnValidation. Thus, the textbox
might contain something different than the model (because its not
verified yet). The textbox content should be pushed in the model after
successful validation.
Now, suppose the label is updated from that status thread. This causes
(as I know from question 1) that all databindings between model and
view are refreshed. Thus, the data binding calls the Text1 getter
(which still contains the old value) and sets it in the textbox (which
we are currently editing). Thus, the textbox value is reset to its
original value.

Obviously, this is a bug and wrong behavior. What can I do to fix it?

Thanks for your time reading this and even more thanks for any answer
you give!

Regards,
Paul

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.