Sharing state between ViewModels

后端 未结 2 969
心在旅途
心在旅途 2021-01-06 14:27

I have two ViewModels that present the same Model to different Views. One presents the model as an item in a ListBox, the other presents it as a tab in a

相关标签:
2条回答
  • 2021-01-06 14:28

    TabControl is ItemsControl, so you shouldn't be shy to bind its ItemsSource to ListBox.SelectedITems.

    Obviously ViewModel for List should have a property that would produce ViewModel for Tabs:

    public TabViewModel ItemTabModel { get { ... } }
    

    And because TabControl is a bit funny, you'd need to add ItemContainerStyle to populate Content for TabControlItem, because the normal ItemTemplate for TableControl only affects headers for tabs.

    0 讨论(0)
  • 2021-01-06 14:53

    Use a ViewModel.

    You've got a View that contains the two controls. Have a view model that will contain a list of ViewModels for the ListBox control to bind to. Also within this view model bind the listbox selection to a second list of viewmodels that the TabControl then also binds to.

    That way your listbox drives what the tab control shows without this information entering the model which should stay oblivious to the existence of the view.

    0 讨论(0)
提交回复
热议问题