WPF TabControl and DataTemplates

后端 未结 5 744
名媛妹妹
名媛妹妹 2021-02-03 11:24

I\'ve got a set of ViewModels that I\'m binding to the ItemsSource property of a TabControl. Let\'s call those ViewModels AViewModel, BViewModel, and CViewModel. Each one of tho

5条回答
  •  借酒劲吻你
    2021-02-03 12:14

    Josh Smith uses exactly this technique (of driving a tab control with a view model collection) in his excellent article and sample project WPF Apps With The Model-View-ViewModel Design Pattern. In this approach, because each item in the VM collection has a corresponding DataTemplate linking the View to the VM Type (by omitting the x:Key as Anderson Imes correctly notes), each tab can have a completely different UI. See the full article and source code for details.

    The key parts of the XAML are:

     
       
     
    
    
    
    

    There is one downside - driving a WPF TabControl from an ItemsSource has performance issues if the UI in the tabs is big/complex and therefore slow to draw (e.g., datagrids with lots of data). For more on this issue, search SO for "WPF VirtualizingStackPanel for increased performance".

提交回复
热议问题