WPF tab control and MVVM selection

前端 未结 4 1386
遥遥无期
遥遥无期 2020-12-29 04:44

I have a TabControl in an MVVM WPF application. It is defined as follows.



        
4条回答
  •  清酒与你
    2020-12-29 05:34

    If you look at the TabControl Class page on MSDN, you'll find a property called SelectedIndex which is an int. Therefore, simply add an int property into your view model and Bind it to the TabControl.SelectedIndex property and then you can select whichever tab you like at any time from the view model:

    
        ...
    
    

    UPDATE >>>

    Setting a 'startup' tab is even easier using this method:

    In view model:

    private int selectedIndex = 2; // Set the field to whichever tab you want to start on
    
    public int SelectedIndex { get; set; } // Implement INotifyPropertyChanged here
    

提交回复
热议问题