forum

Home / DeveloperSection / Forums / WPF basic Binding not working

WPF basic Binding not working

Anonymous User232124-Sep-2013

I am new to WPF and I’ve been breaking my head over this for past couple of days. I am trying to set a basic binding of textbox to a string property. I followed the MS tutorial but nothing seems to be working.

Here's email class, I am trying to bind its subject property to display in a textbox

public class Email : INotifyPropertyChanged

{

    private string _subject;

    public string Subject

    {

        get { return _subject; }

        set

        {

            _subject = value;

            OnPropertyChanged("Subject");

        }

    }

    private string _contents;

    public string Contents

    {

        get { return _contents; }

        set

        {

            _contents = value;

            OnPropertyChanged("Contents");

        }

    }

    private Category _category;

    public Category Category

    {

        get { return _category; }

        set

        {

            _category = value;

            OnPropertyChanged("Category");

        }

    }

    public Email()

    {

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string info)

    {

        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)

        {

            handler(this, new PropertyChangedEventArgs(info));

        }

    }

}

Here's the email setter inside UserControl that parents the textbox:

 private Email _email;

    public Email Email

    {

        get { return _email; }

        set

        {

            _email = value;

            if (_email != null)

            {

                Binding myBinding = new Binding("Subject");

                myBinding.Source = _email;

                tbSubject.SetBinding(TextBlock.TextProperty, myBinding);

            }

        }

    }

tbSubject is never getting set to anything, its always empty even if email passed is not null and has a subject! If I do just this:

public Email Email

    {

        get { return _email; }

        set

        {

            _email = value;

            if (_email != null)

            {

                tbSubject.Text = _email.Subject;

            }

        }

    }

it works fine. I dont understand what I am doing wrong.


wpf
Updated on 24-Sep-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By