-1

I am learning WPF data binding.I have the following scenario. This is my Form1.xaml.cs

 ViewModel1 VM1;
 ViewModel2 VM2;
 Public Form1()
 {
    InitializeComponent();
    VM1 = new ViewModel1();
    VM2 = new ViewModel2;
    this.DataContext=VM1;
 }

Form1.Xaml This works fine(CurrentRec is the currently selected record from an ObservableCollection<> collection of VM1):

<TextBox x:Name="txtTest1" IsEnabled="{Binding CurrentRec.SearchFound}" Text="{Binding CurrentRec.Description1}"/>

This is not working.This second Textbox is bound to a property on the second ViewModel,ie VM2(CurrentRec here is the currently selected record from the ObservableCollection<> collection of VM2):

<TextBox x:Name="txtTest2" DataContext={Binding VM2} IsEnabled="{Binding CurrentRec.SearchFound}" Text="{Binding CurrentRec.Description2}"/>

i even tried this:

 <TextBox x:Name="txtTest2" DataContext={Binding VM2.CurrentRec} IsEnabled="{Binding SearchFound}" Text="{Binding Description2}"/>

but that too hasn't worked for me so far.Plz help.

Jatinder Walia
  • 143
  • 1
  • 12
  • Two things : bound fields need to be public properties, with get/set accessor methods, and bindings always pull from the current DataContext. So you could say `this.DataContext = this`, and then you could bind to both VM1 and VM2 properties. I usually send WPF beginners to [this SO answer](http://stackoverflow.com/a/7262322/302677) to explain DataContext, I'd recommend reading it :) – Rachel May 18 '17 at 13:55

1 Answers1

0
DataContext="{Binding VM2}"

This will work only if VM2 is a property of your DataContext (ViewModel1). WPF will look for the bound properties only in the control DataContext.

If VM2 is a property of Form1, you can try the following:

<TextBox x:Name="txtTest2" DataContext="{Binding VM2, RelativeSource={RelativeSource AncestorType={x:Type local:Form1}}}" IsEnabled="{Binding CurrentRec.SearchFound}" Text="{Binding CurrentRec.Description2}"/

But this is not the best approach. The best way is to make a single DataContext(ViewModel) for each View.

Yevgeniy
  • 1,054
  • 1
  • 9
  • 17
  • Thanks Yevgeniy,it worked...but can u plz throw some light on this RelativeSource thing?I know it shud be asked in a separate thread,but plz would request you if you coud throw some light light on this RelatibveSource thing and why it wud work only if my VM1 was a property and not a normal object...Thanks in advance..:-). – Jatinder Walia May 18 '17 at 14:07
  • When you use something like DataContext="{Binding VM2}", binding is looking for the property in the control DataContext. But you can specify some other source to look in.(ElementName, RelativeSource....) When you use RelativeSource, you can specify some control's ancestor from visual tree. Visual tree is a tree structure of your controls. Look through this post to get more information http://www.c-sharpcorner.com/UploadFile/yougerthen/relativesources-in-wpf/ – Yevgeniy May 18 '17 at 14:43
  • What do you mean saying - "property and not a normal object"? – Yevgeniy May 18 '17 at 14:49
  • thanks Yeggeniy..i got ur point..What i meant by saying normal object was that simple object of any class in code behind..but now that u have made it clear that binding actually looks for a property in code behind,i got it now...thanks once again :-). – Jatinder Walia May 19 '17 at 07:46