PC Review


Reply
Thread Tools Rate Thread

Binding control property to class property?

 
 
Johnny Jörgensen
Guest
Posts: n/a
 
      2nd May 2007
Can anybody help me with this problem, please:


I've got a class

public MyClass
{
private string m_TextValue = "";

public void SetValues()
{
m_TextValue = "Test";
}

public string TextValue
{
get { return m_TextValue; }
set { m_TextValue = value; }
}
}

and in a form I instatiate my class

private MyClass MyClassInstance = new MyClass();

I bind the TextValue property of MyClassInstance to a Textbox' Text field in
the Form_Load event:

textBox1.DataBindings.Add("Text", MyClassInstance, "TextValue");

(I can also do this using the DataBindings field in the Property editor, but
that doesn't change anything)

I would expect textbox1 to show "" now (which it does).

Then I call:
MyClassInstance.SetValues();

Then I would expect textBox1 to show "Test" (which it DOESN'T!)

But if I enter text into textBox1, it is correctly transferred to the
MyClassInstance.TextValue property, so I know the databinding works. What I
don't understand is why the SetValues method changes doesn't reflect in the
bound control's property.

What am I missing?

TIA,
Johnny Jörgensen


 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      2nd May 2007
How would the UI know about the change? You need to implement change
notification. There are 2 *primary* ways of doing this (and various
others);

a: (property specific notification)
add a notification event as follows:
public event EventHandler TextValueChanged;
change your setter to:
set {
if(value == m_TextValue) return; // do nothing if not changed
m_TextValue = value;
EventHandler handler = TextValueChanged;
if(handler!=null) handler(this, EventArgs.Empty); // notify callers
(if any)
}

[although the norm is to split out the bottom 2 into an On... method]

b: (shared notification)
implement the INotifyPropertyChanged, and simlar to above but instead
of EventArgs.Empty you'd create a new
PropertyChangedEventArgs("TextValue")

Marc


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      2nd May 2007
Note... if you use the first approach and you have more than a couple
of events you may wish to look into EventHandlerList; this reduces the
footprint as the class only reserves space for one reference field per
instance and one static reference key per event, not one per event per
instance

(i.e. when nothing is listening your placeholders consume "instances +
events" space instead of "instances * events" space).

Marc


 
Reply With Quote
 
Johnny Jörgensen
Guest
Posts: n/a
 
      2nd May 2007
Thanks Marc

I tried using approach b because I have a lot of properties.

I changed:
public class MyClass
to
public class MyClass : INotifyPropertyChanged

and added:

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}

Then I changed the Set's to:

public string TextValue
{
get { return m_TextValue; }
set
{
if (value == m_TextValue) { return; }
m_TextValue = value;
NotifyPropertyChanged("TextValue");
}
}

But still it doesn't work. What am I missing this time?

TIA,
Johnny J.







"Marc Gravell" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> How would the UI know about the change? You need to implement change
> notification. There are 2 *primary* ways of doing this (and various
> others);
>
> a: (property specific notification)
> add a notification event as follows:
> public event EventHandler TextValueChanged;
> change your setter to:
> set {
> if(value == m_TextValue) return; // do nothing if not changed
> m_TextValue = value;
> EventHandler handler = TextValueChanged;
> if(handler!=null) handler(this, EventArgs.Empty); // notify callers (if
> any)
> }
>
> [although the norm is to split out the bottom 2 into an On... method]
>
> b: (shared notification)
> implement the INotifyPropertyChanged, and simlar to above but instead of
> EventArgs.Empty you'd create a new PropertyChangedEventArgs("TextValue")
>
> Marc
>



 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      2nd May 2007
A very simple oversight; SetValues should read:
TextValue = "Test"
- otherwise none of your fancy new code will ever get called. I have
tested this (below) and it works fine. As a general rule, try to only
talk to fields from property getters/setters (and constructors if
marked readonly) - that way you can't miss this type of thing. Don't
worry about performance: if the getter etc is simple then it will get
inlined by the JIT anyway.

static void Main() {
MyClass c = new MyClass();
using (Form f = new Form())
using (Button b = new Button()) {
f.Controls.Add(b);
f.DataBindings.Add("Text", c, "TextValue");
b.Click += delegate {
c.SetValues();
};
f.ShowDialog();
}
}

Marc

 
Reply With Quote
 
Johnny Jörgensen
Guest
Posts: n/a
 
      3rd May 2007
Thanks Marc - I finally got it to work. I'm so happy! ;-)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Source Control data binding to ActiveX control property Christopher Glaeser Microsoft Access Forms 10 17th Jan 2005 03:19 AM
Parse method never called when data binding from class property to textedit Richard Urwin Microsoft C# .NET 3 15th Oct 2004 04:42 AM
Parse method never called when data binding from class property to textedit Richard Urwin Microsoft Dot NET Framework Forms 1 14th Oct 2004 09:43 AM
Binding to a date class property Tim Delaney Microsoft Dot NET Framework Forms 0 27th Aug 2003 07:53 PM
Binding the Image property of the picturebox to a another class.property Cade Carvell Microsoft VB .NET 1 9th Jul 2003 05:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:51 AM.