0
votes

I have been using Prism's Viewmodel locator to instantiate my viewmodels using the prism naming conventions.I have come unstuck because I have created a user control with it's own viewmodel which I communicate with other viewmodels using the Prism Event Aggregator this is working nicely but I want to create other instances of this user control. Using the event aggregator all instances of the user control are obviously subscribing to the events published so:

  1. How do I differentiate the actual user control that I want to target.
  2. How do instantiate a viewmodel and target a view datacontext without using viewmodel locator?
1

1 Answers

0
votes

What I did was to create a dependency property in the user control called vmType.

    public string vmType
    {
        get { return (string)GetValue(vmTypeProperty); }
        set { SetValue(vmTypeProperty, value); viewModel.vmType = vmType; }
    }

    // Using a DependencyProperty as the backing store for vmType.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty vmTypeProperty =
        DependencyProperty.Register("vmType", typeof(string), typeof(UserControl), new PropertyMetadata(null));

In the set I marked the associated viewmodel property vmType to what the user control was supposed be used for. In the XAMl of the parent view just set vmType towhat ever you need.