1

I've read the online document about data binding but still couldn't make it work. I simply want to understand data binding and do things the way it should.

Here is my code in my UI.xaml.cs

namespace mynamespace
{
   class Customer
   {
     private string _name = "TEST";
     public string Name
     {
        get { return this._name; }
        set { this._name = value; }
     }
   }

   public partial class UI: UserControl
   {
       public UI()
       {
           InitializeComponent();
           Customer c = new Customer();
           this.DataContext = c;
       }
   }
}

The binding code (target is a textbox) looks like this:

<TextBox Name="MyTextBox" Text="{Binding Path=Name}" />

I expect the textbox will show TEST, but it doesn't. There is nothing in the textbox. The documentation (Data Binding Overview, Binding Declarations Overview, and Binding Sources Overview) is not very clear to me.

This is the error message from the Output window:

BindingExpression path error: 'Name' property not found on 'object' ''ConfigurationSettings' (HashCode=36012562)'. BindingExpression:Path=Name; DataItem='ConfigurationSettings' (HashCode=36012562); target element is 'TextBox' (Name='MyTextBox'); target property is 'Text' (type 'String')
7
  • 4
    Why the Window class' constructor is named UI? I think the code can't compile. Commented Sep 9, 2013 at 4:49
  • 2
    Customer class needs to be public? Commented Sep 9, 2013 at 4:50
  • This answer may be helpful. Commented Sep 9, 2013 at 7:59
  • What does the output windows say? Commented Sep 9, 2013 at 8:30
  • The code won't compile in it's current form unless you add a return type to the UI function, in which case it will compile but won't work (the constructor will be the default empty constructor). Change UI to Window in order to make the UI function a constructor for the class Commented Sep 9, 2013 at 8:53

4 Answers 4

1

I am such an idiot! I have two constructors, one default and one parametrized constructor. I was calling the parametrized constructor. After I move the code:

       Customer c = new Customer();
       this.DataContext = c;

to the parametrized constructor, it all worked.

Thank you all for helping and sorry about this stupid mistake.

Sign up to request clarification or add additional context in comments.

Comments

0

Mario is right, so you code should look like this:

namespace mynamespace
{
   class Customer
   {
     private string _name = "TEST";
     public string Name
     {
        get { return this._name; }
        set { this._name = value; }
     }
   }

   public partial class Window : UserControl
   {
       public Window()
       {
           InitializeComponent();
           Customer c = new Customer();
           this.DataContext = c;
       }
   }
}

1 Comment

Mario was right, I was cutting out unrelated code. We source was correct and it did compile. (I also edit the question so it's consistent)
0

If the Customer c is the DataContext shouldnt it be:

 <TextBox Name="MyTextBox" Text="{Binding Name}" />

Comments

0

Try implementing the INotifyPropertyChanged interface and override the ToString() in your Customer class:

public class Customer : INotifyPropertyChanged
{
    private string _name = "TEST";
    public string Name
    {
        get { return _name; }
        set { _name = value; NotifyPropertyChanged("Name"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public override string ToString()
    {
        return Name;
    }
}

Then if that still doesn't work, try setting the Name property with the value in the Window constructor, rather than the private field internally:

public Window()
{
    InitializeComponent();
    Customer c = new Customer() { Name = "TEST" };
    this.DataContext = c;
}

2 Comments

I thought implement INotifyPropertyChanged is when you want to update target and/or source on the fly. Since the Name was set initially, I should at least see TEST. I did implement INotifyPropertyChanged like you suggested, but still no go.
The INotifyPropertyChanged interface is used to update the UI and/or the data sources of any changes... you had better get used to using it, if you want to write WPF. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.