Home > DeveloperSection > Forums > WPF basic Binding not working
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    September-24-2013 8:24 AM

 WPF
Ratings:


 1 Reply(s)
 1016  View(s)
Rate this:

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.



Pravesh Singh

Total Post:411

Points:2881
Posted on    September-24-2013 8:45 AM

Hi Ankita,

I think I've got it. Here's the change I had to make:

    public partial class EmailContentsTemplate : UserControl, INotifyPropertyChanged

{

    private Email _email;

    public Email Email

    {

        get { return _email; }

        set

        {

            _email = value;

            OnPropertyChanged("Email");

        }

    }

    public EmailContentsTemplate()

    {

        InitializeComponent();

        DataContext = this;

        Binding myBinding = new Binding("Email.Subject");

        myBinding.Source = this;

        tbSubject.SetBinding(TextBlock.TextProperty, myBinding);

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string info)

    {

        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)

        {

            handler(this, new PropertyChangedEventArgs(info));

        }

    }

}

So I changed source to "this" and made the user control implement INotifyPropertyChanged. Now it works.

Alternatively I got it working through XAML

<TextBox x:Name="tbSubject" Grid.Column="1" Grid.Row="1" Margin="3" Text="{Binding Email.Subject}"/>


Don't want to miss updates? Please click the below button!

Follow MindStick