1

In my MainWindow I have this:

<Window.DataContext>
    <viewModel:ActiveRecord />
</Window.DataContext>

In my UserControl I have this:

<UserControl.DataContext>
    <search:CustomerSearch />
</UserControl.DataContext>

before I added this 'local' binding i would reference the 'modular' binding in my UserControl like this:

var vm = this.DataContext as ViewModel.ActiveRecord;

but because I want to reference 2 different bindings, it appears to not work any more. What I mean by that is that I populate the ActiveRecord with values but when I navigate to another form these values are null.
What I think I need to do is access the Mainwindow's DataContext like this:

var vm = MainWindow.DataContext as ViewModel.ActiveRecord;

But of course DataContext is not a property of MainWindow

How can I do this?

StepUp
  • 36,391
  • 15
  • 88
  • 148
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • 1
    You should never hardcode the .DataContext like you have in your XAML. It defaults one of the biggest advantages of WPF for having separate layers for UI and data, and can cause all kinds of problems later on. Based on what you've said so far, it sounds like you would be better off having ActiveRecord contain a property for CustomerSearch, and just bind it using an Implicit DataTemplate. That said, Window should have a DataContext property, and I'm pretty sure MainWindow inherits from Window... are you sure you have the right reference? Or just cast it. – Rachel May 12 '16 at 15:31
  • @Rachel thanks for taking the time to comment. I copied the XAML datacontext stuff from a tutorial but now you explained why you should not it makes perfect sense not to do so. The Window object does not expose a DataContext method - is it a static object? I have eventually put the CustomerSearch into the ActiveRecord class. Is there a preferred way/manner in how to set the data context in code for 'modular' scope? Thanks BTW – Andrew Simpson May 12 '16 at 15:37
  • 1
    Typically I would have something like an `AppViewModel` to represent the entire application. From there, each screen of the application usually has it's own ViewModel, such as `HomeViewModel` or `SearchViewModel`. These are properties on AppViewModel. Each screen VM can contain properties for other ViewModels or Models that represent elements on the UI, such as perhaps an ActiveRecord. [This blog post of mine](https://rachel53461.wordpress.com/2011/12/18/navigation-with-mvvm-2/) should give you an idea of what I mean, or perhaps [this answer](http://stackoverflow.com/a/12322363/302677) – Rachel May 12 '16 at 15:48
  • @Rachel ur a star. Thanks again :) – Andrew Simpson May 12 '16 at 15:49

1 Answers1

0

From your user control you can set a relative binding which would look something like this for a Textblock on your user control:

         <TextBlock Text="{Binding Path=DataContext.PropertyYouWant, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}}" />

It does seem to me like you are going about this via the wrong approach though. It would be normal for your user control to inherit its data context from its parent )(i.e. the window). Would it be possible to give the active record access to the Customer Search?

G Lewis
  • 81
  • 4
  • Hi, thanks for your answer. I DID have it that way round and everything inherited from the main datacontext but I was worried that my VM would get too big. Your proposed solution does not solve my probelm and I am aware of using relative. The issue I have is referencing the MainWindow Datacontext via code in a UserControl that has its own datacontext set – Andrew Simpson May 12 '16 at 14:37