简体   繁体   English

使用MEF注入viewmodel以进行查看-从源进行绑定不起作用-WPF

[英]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: 我正在使用MEF容器将视图模型注入视图,如下所示:

    [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. 我在view和viewmodel上都具有export属性。

The problem is that when a value\\property is updated on the ViewModel by some method, it does not reflect on the UI. 问题在于,通过某种方法在ViewModel上更新值\\属性时,它不会反映在UI上。 I even tried to set Mode to Two way - still not working. 我什至尝试将“模式”设置为“双向”-仍然无法正常工作。 I have OnPropertyChanged called for all property setters, but not working. 我已经OnPropertyChanged呼吁所有属性设置器,但无法正常工作。 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. 但是,尽管在视图后面的代码中,如果我将一个propertychanged更改的事件方法附加到viewmodel上,则会收到事件通知,这是我能够使用它们来更新视图的唯一方法,但这不是必需的。 I just don't know why my view is not responding to propertychanged notifications directly on the bound elements. 我只是不知道为什么我的视图不直接在绑定元素上响应propertychanged更改的通知。

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. 原来,在我的抽象ViewModelBase类中,我具有了notifypropertychanged的函数...但是,尽管我实现了方法..stupid错误,但在类名上却没有INotifyPropertyChanged。 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. 现在工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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