0

I have a Class:

public class Person {
    public string Name { set; get; }
}

And i instantiate 2 persons from that class:

Person person1 = new Person() { Name="Test1" };
Person person2 = new Person() { Name="Test2" };

And in my Window.xaml i have two textboxes that I want to bind with the two person objects

<Grid>
    <TextBox />
</Grid>

<Grid>
    <TextBox/>
</Grid>

But I am new in WPF and don't know how to do.

I tried

<Grid DataContext="{Binding person1}">
    <TextBox Text="Binding Name"/>
</Grid>

<Grid DataContext="{Binding person2}">
    <TextBox Text="Binding Name"/>
</Grid>

Didn't worked. Tried

<Grid>
    <TextBox Text="Binding person1.Name"/>
</Grid>

<Grid>
    <TextBox Text="Binding person2.Name"/>
</Grid>

Didn't work.

I can bind one person setting DataContext in Window1.cs

But I can't (or don't know how) to set the 2 person as DataContex in Window1.

DasSoftware
  • 964
  • 10
  • 19
Eduvm
  • 1,131
  • 8
  • 14
  • WPF data binding works with public properties only. Turn your fields or local variables into properties, e.g. `public Person Person1 { get; } = new Person() { Name="Test1" };`, then set the DataContext of the Window to itself, i.e. set `DataContext = this;`. Now bind like ``. – Clemens Mar 26 '19 at 06:40
  • @Clemens It's a totally different thing. how to bind multiple objects. – Hemang A Mar 26 '19 at 06:43
  • Please read Edvum his lines. I can bind one person setting DataContext in Window1.cs But i cant (or dont know how) to set the 2 person as DataContex in Window1. – Hemang A Mar 26 '19 at 06:44
  • Alternatively create another class with these two Person properties (we typically call it a view model), and assign the DataContext to this class like `DataContext = new ViewModel();`. – Clemens Mar 26 '19 at 06:44
  • If you want to bind a variable number of TextBoxes to a collection of Persons, use an ItemsControl with the TextBox in its ItemTemplate, and bind its ItemSource to an `ObservableCollection` property in the view model. – Clemens Mar 26 '19 at 06:47
  • if many fields come to the same name, age, date of birth. So, every property has twice created. – Hemang A Mar 26 '19 at 06:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190672/discussion-between-hemang-aghera-and-clemens). – Hemang A Mar 26 '19 at 07:08

1 Answers1

-2

Please first set data context like this.

Person person1 = new Person() { Name="Test1" };
Person person2 = new Person() { Name="Test2" };

DataContext =  new           
{
  Person1 = person1,
  Person2 = person2 
};

After designing side bind data like this.

<Grid>
    <TextBox Text="{Binding Person1.Name,Mode=TwoWay, 
                                  UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

<Grid>
    <TextBox Text="{Binding Person2.Name,Mode=TwoWay, 
                                  UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

OR (You can change only in design side like this)

<Grid DataContext="{Binding person1}">
    <TextBox Text="{Binding Name,Mode=TwoWay, 
                                      UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

<Grid DataContext="{Binding person2}">
      <TextBox Text="{Binding Name,Mode=TwoWay, 
                                      UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
Hemang A
  • 1,012
  • 1
  • 5
  • 16
  • While you correctly noticed that there should be two properties in the Window's DataContext, these should of course not be in an anonymous class, because that would make it hard to access them later. The standard approach is to declare a view model class with these properties, and assign an instance of the view model to the DataContext. – Clemens Mar 26 '19 at 06:54
  • if one class has used multiple places(windows) then every time you have to set same class and output comes as same and bind the same in multiple forms then what to do? – Hemang A Mar 26 '19 at 06:56
  • Reuse the view model instance and assign it to the DataContext of multiple views. – Clemens Mar 26 '19 at 07:01
  • public class ViewModel { public Person objperson {get;set;} public PersonDetail objperson {get;set;} } public class ViewModel1 { public Person objperson {get;set;} public FamilyDetail objperson {get;set;} } In this viewmoedl every time comes multiple class and one is common and another is different.So, What's easiest way ? – Hemang A Mar 26 '19 at 07:04
  • Thank you @HemangAghera With your example i understood that i need to use MVVP pattern and created a ViewModel Class to hold person1 and person2 as properties, then setted this ViewModel as data context and everything is working now. – Eduvm Mar 26 '19 at 21:15