56889

Is it possible to Bind the DataContext of a UserControl to a property

Question:

I have a user control that I would like to use in two different contexts. The user control needs its DataContext set to an instance of an appropriate ViewModel that has been created by the parent view/viewmodel.

I was hoping for something like:

<local:Child DataContext="{Binding ChildViewModel}"/>

where ChildViewModel is a inpc-styled property of the ViewModel that the page is bound to.

That doesn't seem to work. Is it possible to assign the DataContext by using Binding?

Answer1:

It would probably be simpler to bind the Content of an ContentControl to your child ViewModel like this:

<ContentControl Content="{Binding ChildViewModel}" />

..and then have a DataTemplate to apply your local:Child View, like this

<DataTemplate DataType="{x:Type local:ChildViewModel}"> <local:Child /> </DataTemplate>

Recommend

  • Passing arrays vs structures to functions
  • Why is an execution context not created here?
  • Get Image uint8 data without canvas
  • error when accessing worklight server deployed on tomcat
  • How to prevent context menu in an iframe?
  • Do I substract 1 from 45 each time a student registers for a seat?
  • Uncaught DOMException: Failed to construct 'AudioContext': The number of hardware contexts
  • IE7 dropdown menu appears behind image
  • OpenCL: Correct results on CPU not on GPU: how to manage memory correctly?
  • Comma in C/C++ macro
  • How to hide application context in its URL with Tomcat?
  • How to insert a c# datetime var into SQL Server
  • Calling a CUDA-enabled library in a new thread
  • Solve apparent need for outside reference to entity inside aggregate (DDD)
  • _Layout.cshtml cannot be requested directly because it calls the “RenderBody” method
  • Cannot convert value of type 'X' to expected argument type 'X'
  • SQL Server Integrated Security from an Azure Web Site
  • Furthest-point Voronoi diagram in Java
  • Merge Module leaving files during uninstall
  • Why doesnt this Java loop in a thread work?
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Creating a DropDownList
  • Redirect STDERR in OPEN pipe comand. Perl Linux
  • Force show.bind execution
  • PHP buffered output depending on server setting?
  • Get one-time binding to work for ng-if
  • Xamarin Forms - UWP Fonts
  • Pass value from viewmodel to script in zk
  • QuartzCore.framework for Mono Develop
  • Cannot Parse HTML Data Using Android / JSOUP
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • Linking SubReports Without LinkChild/LinkMaster
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?