---
title: "WPF basic Binding not working"  
description: "WPF basic Binding not working"  
author: "Anonymous User"  
published: 2013-09-24  
updated: 2013-09-24  
canonical: https://www.mindstick.com/forum/1514/wpf-basic-binding-not-working  
category: "wpf"  
tags: ["wpf", "mindstick", "WPF basic Binding not working", "binding", "wpf binding"]  
reading_time: 2 minutes  

---

# WPF basic Binding not working

I am new to WPF and I’ve been breaking my head over this for past couple of days. I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to set a basic [binding](https://www.mindstick.com/blog/146/dynamic-binding-in-c-sharp) of [textbox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview) to a string [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp). I followed the MS tutorial but nothing seems to be working.

Here's email class, I am trying to bind its [subject](https://yourviews.mindstick.com/view/259/physical-education-a-compulsory-subject-in-school-curriculum) property to display in a textbox

public class Email : INotifyPropertyChanged

{

[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) 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](https://www.mindstick.com/forum/12739/set-the-enabled-property-of-a-usercontrol-from-codebehind) 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](https://www.mindstick.com/interview/1933/what-is-not-null-constraint) 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](https://answers.mindstick.com/qa/115732/my-software-works-fine-on-windows-10-but-crashes-on-windows-11-why). I dont understand what I am [doing wrong](https://answers.mindstick.com/qa/36736/what-are-indians-doing-wrong-on-whatsapp).

## Replies

### Reply by Pravesh Singh

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}"/>


---

Original Source: https://www.mindstick.com/forum/1514/wpf-basic-binding-not-working

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
