Control bind to object does not refresh it's display

M

Mikus Sleiners

I have a control - textBox1 that is binded to objects propery - "Currency"
and another control - textBox2 (read only) that is also binded to same
propery.
Now, i have a situation where textbox1 control does NOT refresh it's display
according to value set to underlying propery while at the same time
textBox2 does refresh accordingly.

Both textboxes ar located on the same form and there are no diferences in
their bindings.

private int _currency = (int)cur.LVL;

public int Currency
{
get { return _currency; }
set
{
if(!_currency.Equals (value))
{
if(value == 2)
{
// Consider according to some business logic not shown here,
value should be changed from 2 to 3
value = 3;
}

_currency = value;

if(PropertyChanged != null)
{
PropertyChanged (this, new PropertyChangedEventArgs
("Currency"));
}
}
}
}

The scenario that produces incorrect response is following:

1) By default when i open form propery has value of 1, so both textboxes
contain value of 1 (textBox2 is readonly)
2) I change value in textBox1 from 1 to 3 => binding does it's job and both
textBoxes now contain value of 3. This is correct
3) Now i change value in textBox1 from 3 to 1 => binding does it's job and
both textBoxes now contain value of 1. This is also correct
4) Now let's change value in textBox1 from 1 to 2 (as you can see in code
above - value of 2 will not be set as it get's changed by business logic
from 2 to 3, so 3 is set) =>
now textBox2 displays correct value of 3, but textBox1 still shows 2... it
looks like binding does not refresh on control that have initialized the
change of value.

I tried to rewrite property this way:

public int Currency
{
get { return _currency; }
set
{
if(!_currency.Equals (value))
{
_currency = value;

if(PropertyChanged != null)
{
PropertyChanged (this, new PropertyChangedEventArgs
("Currency"));
}

if(value == 2)
{
_currency = 3;
}

if(PropertyChanged != null)
{
PropertyChanged (this, new PropertyChangedEventArgs
("Currency"));
}
}
}
}

but it does not help either.

Final info: my object class is inherited from INotifyPropertyChanged and
does contain :
// Event for handling data changes
public event PropertyChangedEventHandler PropertyChanged;

Any ideas on how to solve this ?
 
W

Walter Wang [MSFT]

Hi Mikus,

It seems related to the 4th parameter of Bindings.Add():

textBox1.DataBindings.Add("Text", c1, "Currency", true);
textBox2.DataBindings.Add("Text", c1, "Currency", false);

With above change, it will also reproduce the issue you're experiencing.
The code generated by the designer is using "true".


It's actually a known issue:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?Feedbac
kID=115342

According to current status of this bug report, I'm afraid you'll have to
use the workaround I'm using here: setup the DataBinding manually or reset
the FormattingEnabled property in Form_Load:

foreach (Binding b in textBox1.DataBindings)
{
b.FormattingEnabled = false;
}
foreach (Binding b in textBox2.DataBindings)
{
b.FormattingEnabled = false;
}

Property FormattingEnabled is new feature in .NET 2.0. It basically means
go through the new improved code path for databinding that we introduced in
VS2005.

I hope above explanation could help you. Please reply to let me know
whether or not you need further information. Thank you.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Mikus,

Looks like my workaround will cause the other issue as you mentioned in
[3]. Please see my reply there for another workaround.

References:

[1] #Control bind to object does not refresh it's display in
dotnet.languages.csharp
http://msdn.microsoft.com/newsgroups/managed/default.aspx?dg=microsoft.publi
c.dotnet.languages.csharp&tid=31384fbc-a888-4286-b6c9-c3be53e77e5e&m=1&p=1

[2] #Feedback: Current control doesn't refresh values due to
PropertyChanged event
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?Feedbac
kID=115342

[3] #Binding behaviour regarding exceptions in dotnet.languages.csharp
http://msdn.microsoft.com/newsgroups/managed/default.aspx?dg=microsoft.publi
c.dotnet.languages.csharp&mid=72e21545-078c-4676-96aa-46c481827590&sloc=en-u
s&m=1

[4] #Binding.FormattingEnabled Property (System.Windows.Forms)
http://msdn2.microsoft.com/en-us/library/system.windows.forms.binding.format
tingenabled.aspx

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top