Jun 23, 2009 at 9:03 PM
Edited Jun 23, 2009 at 11:22 PM
If I understood you scenario correctly, you want to create a UserControl that binds
to data provided by a service. If that is the case, there are 3 approaches that I can think of that might be suitable in your scenario:
Instantiate the UserControl with the container
You can achieve this using the Resolve method of the
YourUserControl yourControl = unityContainer.Resolve<YourControl>();
This will allow you to inject dependencies though the User control constructor (this is how views are injected). As you said, the drawback of this
approach is that you won't be able to declare the usercontrol in XAML.
Create DependencyProperties in the control and bind to them from the view
When you define a UserControl you can define
DependencyProperties. DependencyProperties are like .Net usual properties,
but support databinding, animation, etc. You can read further here: Control Authoring Overview.
So for example, if your service provides a customer which your control needs to bind to, you can create a Customer DependencyProperty in the control
and bind that property to the customer returned by the service.
Create a IUnityContainer property in the control
If you define a
public IUnityContainer property in the userControl, you can then set it from the view. This might not be a good idea if you are planning to reuse the control in other scenarios, as you would be coupling the control to
the container. It would be better to make the control depend on the service, or if you want a fully reusable control use the 2nd approach.
Hope it helps!