简体   繁体   中英

Injecting viewmodel to view with MEF - Binding from source not working - WPF

I'm injecting my viewmodel to my view using the MEF container like below:

    [ImportingConstructor]
    public MainView(IRepository repository, MainViewModel viewModel)
    {
        InitializeComponent();
        mMainViewModel = viewModel;
        DataContext = viewModel;
        viewModel.PropertyChanged += OnViewModelPropertyChanged;
    }

Everything works fine. I have the export attribute on both the view and viewmodel.

The problem is that when a value\\property is updated on the ViewModel by some method, it does not reflect on the UI. I even tried to set Mode to Two way - still not working. I have OnPropertyChanged called for all property setters, but not working. HOWEVER, in the code behind for the view though, if I attach a propertychanged event method to the viewmodel, I get the event notifications and that is the only way I'm able to update the view using their but that should not be necessary. I just don't know why my view is not responding to propertychanged notifications directly on the bound elements.

What could I be doing wrong?

Found the issue. Turned out that in my abstract ViewModelBase class, I had the functions for notifypropertychanged...But I didn't have the INotifyPropertyChanged on the class name although I implemented the method..stupid mistake. Must have missed it. Brings up another question...why must we have the interface definition on the class for it to work?...well, all is good now. works fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM