从 ViewModel 选择 TabControl 中的 TabItem

Selecting TabItem in TabControl from ViewModel

我已经为此苦苦挣扎了一天左右,无法弄清楚我在这里做错了什么。我希望能够 select 我的可观察选项卡集合中的任何选项卡,并且我希望我的 selection 在 UI 中可见。我试过 SelectedIndex 和 SelectedItem。我可以看到我的属性已设置,但我的选项卡未 selected,UI 中没有任何反应。这是我的代码:

MainWindow.xaml

<Window x:Class="WpfApplication5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:uc="clr-namespace:WpfApplication5"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <ViewModel xmlns="clr-namespace:WpfApplication5" />
</Window.DataContext>
<StackPanel>
    <Button Content="Select Tab Index 0" Click="Button_Click_0"/>
    <Button Content="Select Tab Index 1" Click="Button_Click_1"/>
    <Label Content="{Binding SelectedIndex, UpdateSourceTrigger=PropertyChanged}" />
    <TabControl ItemsSource="{Binding Tabs}" SelectedIndex="{Binding SelectedIndex, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Header}"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <uc:TabContent Content="{Binding Content}"/>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</StackPanel>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click_0(object sender, RoutedEventArgs e)
    {
        var viewModel = (ViewModel)DataContext;
        viewModel.SelectedIndex = 0;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var viewModel = (ViewModel)DataContext;
        viewModel.SelectedIndex = 1;
    }
}

ViewModel.cs

class ViewModel
{
    private int _selectedIndex = 0;
    public event PropertyChangedEventHandler PropertyChanged;
    private ObservableCollection<Tab> _tabCollection = new ObservableCollection<Tab>();

    public ViewModel()
    {
        Tabs.Add(new Tab { Header = "Tab1", Content = new WpfApplication5.TabContent() });
        Tabs.Add(new Tab { Header = "Tab2", Content = new WpfApplication5.TabContent() });
    }

    public ObservableCollection<Tab> Tabs
    {
        get { return _tabCollection; }
    }

    public int SelectedIndex
    {
        get { return _selectedIndex; }
        set
        {
            _selectedIndex = value;
            NotifyPropertyChanged("SelectedIndex");
        }
    }

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Tab.cs

class Tab
{
    public UserControl Content { get; set; }
    public string Header { get; set; }
}

TabContent.xaml

<Grid>
    <Label Content="Hello World!" />
</Grid>

您的 ViewModel class 没有实现 INotifyPropertyChanged 接口:

class ViewModel : INotifyPropertyChanged
{
    ...

那是你的问题。