0
votes

I create a WPF application using Prism and MEF. In one module, I have a Customer list. To edit customers data, I have one View "EditCustomer.xaml".

A user can edit many customers, so he can open many different instances of EditCustomer.xaml.

Here's code of the ViewModel :

[Export(typeof(EditCustomerViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class EditCustomerViewModel : ViewModelBase

And the code behing of XAML :

[Export(typeof(EditCustomerView))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class EditCustomerView : RadPane
{
    public EditCustomerView()
    {
        InitializeComponent();
    }

    [Import(typeof(EditCustomerView))]
    public object ViewModel
    {
        get
        {
            return this.DataContext;
        }
        set
        {
            this.DataContext = value;
        }
    }
}

The "CreationPolicy.NonShared" property works perfectly, but when I want to bind the customers data, every views are binded with the last values. I only want to load and display the latest data in the new view instance.

How can it be possible? Maybe I forgot something ?

In advance, thanks for your help.

Regards.

1
what happens if you use [ImportingContructor] instead of import with property? - blindmeis
I think the problem comes from my EventAggregator : - When I want to open the view to edit my Customer data, I call an event aggregator to load the data in the view - I reuse the same view to Create and Edit So.. maybe the event aggregator update every view instances when its called? - GuillaumeL

1 Answers

0
votes

I have a menu, where I load the views by clicking on items, this code comes from my ShellViewModel:

private void ShowView(object viewName)
{
    IRegion region = this.regionManager.Regions[RegionConstants.CentralRegionName];

    object view = null;

    try
    {
        switch (viewName as string)
        {
            case ViewConstants.CustomerListViewName:
                view = ServiceLocator.Current.GetInstance<CustomerListView>();
                break;
            default:
                return;
        }

    if (region.Views.Contains(view))
    {
        region.Remove(view);
    }

    region.Add(view);
    region.Activate(view);
}

And, in my behaviors in each module, I can load other views from contextual menu like this :

IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
                IRegion region = regionManager.Regions[RegionConstants.CentralRegionName];

                object view = null;
                switch (header)
                {
                    case "Update":
                        view = ServiceLocator.Current.GetInstance<UpdateCustomerView>();
                        IEventAggregator eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
                        eventAggregator.GetEvent<UpdateCustomerEvent>().Publish(new UpdateCustomerEvent { ID = custId});
                        break;
                    default:
                        break;
                }

                if (view != null)
                {
                    if (region.Views.Contains(view))
                    {
                        region.Remove(view);
                    }

                    region.Add(view);
                    region.Activate(view);
                }