tags:

views:

70

answers:

2
A: 

You really need the INotifyPropertyChanged interface:

using System.ComponentModel;

namespace TestBindingApp
{
public class Class1
{
    // Singleton instance
    private static Class1 instance;
    private string _accountNumber;
    public Class1()
    {

    }

    // Singleton instance read-only property
    public static Class1 Instance
    {
    get
    {
        if (instance == null)
        {
        instance = new Class1();
        }
        return instance;
    }
    }

    public string AccountNumber
    {
    get
    {
        return _accountNumber;
    }
    set
    {
        if (value != _accountNumber)
        {
        _accountNumber = value;
            NotifyPropertyChanged("AccountNumber");
        }
    }
    }

        public event PropertyChangedEventHandler PropertyChanged;


    private void NotifyPropertyChanged(string property)
    {
        if(PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
}
Arcturus
I have tried this approach before, but it gives me an error - "An object reference is required for the non-static field, method, or property 'TestBindingApp.Class1.NotifyPropertyChanged(string)'". Looks like it is because of the static class. Is there a different way to implement INotifyPropertyChanged for a staic property?
Abhi
Please note the if(PropertyChanged != null) in NotifyPropertyChanged.. Its there for a reason ;)
Arcturus
Sorry Arcturus, I didn't get your meaning. Couldn't post my updated code in the comments, so updated my original post.My updated code has the "if(PropertyChanged != null)", but it gives me an error - "An object reference is required for the non-static field, method, or property 'TestBindingApp.Class1.NotifyPropertyChanged(string)'". . I have just started learning WPF, so if you could explain in detail, that would be very helpful. Thanks for your patience.Thanks, Abhi.
Abhi
Make the property non static.. And it should work! :)
Arcturus
Also, I prefer to set the PropertyChanged to an empty delegate to start with - then I never have to worry about null checks: public event PropertyChangedEventHandler PropertyChanged = delegate { };
Lunivore
If I give "txtBankNumber.GetBindingExpression(TextBox.TextProperty).UpdateTarget();" after changing value of AccountNumber property, the textbox property changes fine. That means that the binding is setup properly, but the NotifyPropertyChanged event is not working properly because of something that I need to do. Any other ideas?
Abhi
Did you make both your field and property non static ?
Arcturus
Nope, I didn't changed them to non-static as there are a few fields that I have to keep static. I am looking for a way to accomplish twoWay binding with static properties. I can do that with updating target from the code-behind using "txtBankNumber.GetBindingExpression(TextBox.TextProperty).UpdateTarget()", but thats not ideal and it would be same as manually setting the text property of the textbox.
Abhi
I dont see why the property needs to be static, when you can access them through the Instance as well.. In stead of Class1.AccountNumber, you can also do Class1.Instance.AccountNumber when the property is non static.
Arcturus
Ohh, I didn't thought of that, my bad!! I will try that. Thanks a lot Arcturus.
Abhi
Hi Arcturus, I have updated my original post with my latest code. It is still not behaving as I expect it to. Am I expecting it to do something which it isn't meant to do, or am I doing something wrong.
Abhi
also change your field to non static: private static string _accountNumber; should be private string _accountNumber;
Arcturus
I tried that, but when I changed it to non-static the value in the textbox never changed. It shows up as blanks on the page.
Abhi
Your binding needs to change as well:<Binding Source="{StaticResource TClass}" Path="Instance.AccountNumber" Mode="TwoWay" NotifyOnValidationError="True" UpdateSourceTrigger="PropertyChanged">
Arcturus
Thanks man, it worked. You are a champion. :)
Abhi
A: 

You call NotifyPropertyChanged from a static member, but NotifyPropertyChanged itself isn't static.

Two ways to solve: Either make AccountNumber NOT static or provide an instance for your call to NotifyPropertyChanged (e.g. "Instance.NotifyPropertyChanged(...)")

HDW
I can't change the property to non-static because that will not serve it's purpose. I changed NotifyPropertyChanged("AccountNumber") to Instance.NotifyPropertyChanged("AccountNumber"). The application builds fine, but it still doesn't work. When it goes inside the NotifyPropertyChanged method, PropertyChanged is null and it doesn't change the textbox value. Any ideas?
Abhi
If I give "txtBankNumber.GetBindingExpression(TextBox.TextProperty).UpdateTarget();" after changing value of AccountNumber property, the textbox property changes fine. That means that the binding is setup properly, but the NotifyPropertyChanged event is not working properly because of something that I need to do. Any other ideas?
Abhi