Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binding to a property of an object

Tags:

c#

mvvm

binding

wpf

I want to bind a series of TextBoxes in a grid into properties of an object which is itself another property in my ViewModel (the DataContext).

CurrentPerson consists of Name and Age properties

Inside the ViewModel:

public Person CurrentPerson { get; set ... (with OnPropertyChanged)}

Xaml :

<TextBox Text="{Binding Name}" >
<TextBox Text="{Binding Age}" >

I wasn't sure on the approach to use, I set another DataContext in the grid scope, without any result, Also tried setting the source and path like Source=CurrentPerson, Path=Age again without any result, these were for trial and see if there would be any change or not.

How should I achieve this ?

like image 666
LastBye Avatar asked Mar 25 '13 08:03

LastBye


People also ask

What do you mean data binding?

What is data binding? Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.

How do you bind an object in C#?

The INotifyPropertyChanged interface is used to notify a 'binding' that a property has changed, so the DataBinding infrastructure can act accordingly to it. Then, you can databind the MyName property to the Text Property of the textbox.

What is binding in C#?

When an object is assigned to an object variable of the specific type, then the C# compiler performs the binding with the help of . NET Framework. C# performs two different types of bindings which are: Early Binding or Static Binding. Late Binding or Dynamic Binding.

How do you bind property in XAML?

One-Way Data Binding The following XAML code creates four text blocks with some properties. Text properties of two text blocks are set to “Name” and “Title” statically, while the other two text blocks Text properties are bound to “Name” and “Title” which are class variables of Employee class which is shown below.


2 Answers

Does your Person class members Name and Age raise INPC themselves?

If you want to update the value of either Name or Age in the ViewModel and have it reflect in the view, you need them to raise property changed individually inside Person class as well.

The bindings are fine, but the view is not notified of changes from the view model. Also remember UpdateSourceTrigger for a TextBox by default is LostFocus, so setting that to PropertyChanged will update your string in the ViewModel as you're typing.

Simple example:

public class Person : INotifyPropertyChanged {
  private string _name;
  public string Name {
    get {
      return _name;
    }

    set {
      if (value == _name)
        return;

      _name = value;
      OnPropertyChanged(() => Name);
    }
  }

  // Similarly for Age ...
}

Now your xaml would be:

<StackPanel DataContext="{Binding CurrentPerson}">
  <TextBox Text="{Binding Name}" />
  <TextBox Margin="15"
            Text="{Binding Age}" />
</StackPanel>

or you can also bind as suggested by @Kshitij:

<StackPanel>
  <TextBox Text="{Binding CurrentPerson.Name}" />
  <TextBox Margin="15"
            Text="{Binding CurrentPerson.Age}" />
</StackPanel>

and to update view model as you're typing:

<StackPanel DataContext="{Binding CurrentPerson}">
  <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
  <TextBox Margin="15"
            Text="{Binding Age, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
like image 81
Viv Avatar answered Oct 12 '22 22:10

Viv


Try this:

<TextBox Text="{Binding CurrentPerson.Name}" />
<TextBox Text="{Binding CurrentPerson.Age}" />

Essentially, you can drill down into properties by using the . separator.

For future reference, if you want to drill down into collections, you can use MyCollection[x] just like you would in code (where x would be replaced by a hard-coded number, not a variable).

like image 41
K Mehta Avatar answered Oct 12 '22 20:10

K Mehta