Question about ViewModel Management (DesignTime Vs Run Time)

前端 未结 3 1018
庸人自扰
庸人自扰 2020-12-31 13:42

I have a fairly basic WPF UI whereby user requests cause a new tab to open in my TabControl. The TabControl is bound to an ObservableCollection

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 14:28

    Yes I think it will interfere with your current setup

    The ViewModelLocator is a static class that returns a dummy object at design time, and a static ViewModel at runtime. This means that

    • The ViewModelLocator, not your ParentViewModel, contains your TabViewModels

    • You cannot have multiple instances of the same Tab (ViewModel) open at once

    • You cannot manage Open/Closed tabs unless you reference the UserControl, which is a violation of the MVVM principle where the ViewModel doesn't know of the View

    • You can't instantiate new copies of the TabViewModel with parameterized constructors. For example, OpenTabs.Add(new CustomerViewModel(CustomerId));

    Perhaps an alternative could be a Converter? One that returns a static object if in design time, or the bound object during runtime? I've never tested such a thing but in theory it should work :)

提交回复
热议问题