从ViewModel中选择TabControl中的TabItem

时间:2017-08-29 09:00:54

标签: c# wpf xaml mvvm tabcontrol

我一直在努力奋斗一天左右,无法弄清楚我在这里做错了什么。我希望能够在我的可观察选项卡集合中选择任何选项卡,并且我希望我的选择在UI中可见。我尝试过SelectedIndex和SelectedItem。我可以看到我的属性已设置但我的选项卡未被选中,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>

1 个答案:

答案 0 :(得分:1)

您的ViewModel课程没有实现INotifyPropertyChanged界面:

class ViewModel : INotifyPropertyChanged
{
    ...

那是你的问题。