简体   繁体   中英

UserControl design

I have to write a new UserControl for AccountManagement. To integrate in your application with several TabItem s my UserControl has to implement the IModule interface which looks something like:

public interface IModule : INotifyPropertyChanged
{
  FrameworkElement TabContent { get; }
  ImageSource TabIcon { get; }
  string TabTitle { get; }
}

I want to build the UserControl for AccountManagement with MVVM so I want to implement the IModule interface in my ViewModel.

My two questions are:

  1. Is this a good solution, or should I prefer to implement IModule in my View-CodeBehind?

  2. If I implement IModule in the ViewModel, how can I pass the View as TabContent to the parent?

No. FrameworkElement is a view concept and should not be in the ViewModel. I assume you will have to implement it in code-behind, but why don't you see how other tabs are written and ensure you are consistent with the existing codebase?

EDIT: You will still need to implement the above in the View. All three properties are view based, with the possible exception of TabTitle which could pull its name from the view model. There is nothing stopping you moving toward MVVM for the content of the tab, but this existing requirement will have to be done in the view.

A good rule of thumb for deciding if something belongs in the VM is to consider whether you could unit test the VM without any view at all, or whether you could theoretically write a text based console view to drive the VM.

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