从 ViewModel 中选择 TabControl 中的 TabItem [英] Selecting TabItem in TabControl from ViewModel

查看:31
本文介绍了从 ViewModel 中选择 TabControl 中的 TabItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been struggling with this for a day or so, can't figure out what I'm doing wrong here. I want to be able to select any tab in my observable collection of tabs, and I want my selection to be visible in the UI. I have tried SelectedIndex and SelectedItem. I can see that my Properties are set but my tabs are not selected, nothing happens in the UI. Here is my code:

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 类没有实现 INotifyPropertyChanged 接口:

Your ViewModel class doesn't implement the INotifyPropertyChanged interface:

class ViewModel : INotifyPropertyChanged
{
    ...

那是你的问题.

这篇关于从 ViewModel 中选择 TabControl 中的 TabItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆