-1

I'm loading UserControl(s) into a shell window using DataTemplate(s) (View Model First). How can I get a reference to the UserControl that is currently contained in that Window?

5
  • 1
    Why do you need the UserControl and not the ViewModel?
    – Barracoder
    Mar 13, 2014 at 13:17
  • when you do View Model First then you load a viewmodel(not a usercontrol) and bind it to a contentcontrol.content property. wpf do the rest and search for a datatemplate to show you your viewmodel within your usercontrol.
    – blindmeis
    Mar 13, 2014 at 13:57
  • Why down vote?? @ Mark Green: I need a referene to the user control because there's a strictly-UI operation that's need to be done- ONLY in the WPF client. @blindmeis: I know how things works in View Model First, and I still need to use the current user control.
    – Nir Smadar
    Mar 15, 2014 at 22:36
  • even with a datatemplate a usercontrol fires onload event. so who need a reference to the usercontrol?
    – blindmeis
    Mar 16, 2014 at 12:26
  • @blindmeis: It's not a question of why do I need the reference, or is it MVVM compatible- it's a question of how to get it. onload will not work here.
    – Nir Smadar
    Mar 17, 2014 at 7:03

1 Answer 1

0

You can grab current application window:

Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);

And then you could check its content/children. But that will be breaking MVVM pattern which I guess you are trying to achieve. In cases where you need to hook up something to a usercontrol specific stuff, you should use interaction behaviors.

1
  • No good- those proprties will not give me the current user control. I'm fully aware that this breaks the pattern, but it's not a bad practice in this case- I need to perform a UI operation that's uniqe to the WPF client.
    – Nir Smadar
    Mar 15, 2014 at 22:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.