wpf - 如何从我的 ViewModel 跟踪选定的 TabPage 的 DataContext?

标签 wpf mvvm tabcontrol tabitem

我有 TabItem 类:

 public class TabItem
{
    public string Header { get; set; }
    public IView Content { get; set; }
}

在我的模型中:
 public ObservableCollection<TabItem> Tabs
    {
        get { return _tabs; }
        set
        {
            if(_tabs!=value)
            {
                _tabs = value;
                RaisePropertyChanged("Tabs");
            }
        }
    }

    public TabItem CurrentTabItem
    {
        get { return _currentTabItem; }
        set
        {
            if (_currentTabItem != value)
            {
            }
            _currentTabItem = value;
            RaisePropertyChanged("CurrentTabItem");
        }
    }

在 View 中我绑定(bind)到 ModelView:
<TabControl x:Name="shellTabControl" ItemsSource="{Binding Tabs}" 
            IsSynchronizedWithCurrentItem="True" SelectionChanged="ShellTabControlSelectionChanged">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Header}"/>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <ContentPresenter Content="{Binding Content}"/>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

从 View 中我想更改 ViewModel 的 CurrentTabItem 属性:
 private void ShellTabControlSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(e.Source is TabItem)
        {
            var tabItem = e.Source as TabItem;
            ViewModel.CurrentTabItem = tabItem; //don't work
        }
    }

将 TabControl 的 TabItem 转换为我的 TabItem 的最佳方法是什么?

最佳答案

也许最好使用 SelectedItem="{Binding CurrentTabItem, Mode=TwoWay, UpdateSourceTrigget=PropertyChanged}"?

关于wpf - 如何从我的 ViewModel 跟踪选定的 TabPage 的 DataContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8147629/

相关文章:

c# - 当需要该类 WPF 的多个实例时,模型内部的 INotifyPropertyChanged

c# - 来自 C# 的 DirectX

c# - 如何动态创建功能区选项卡?

c# - 动态添加选项卡到 TabControl 容器

c# - 如何在 C# 中隐藏/阻止选项卡?

wpf - WPF图像,如何消除模糊?

c# - 在 WPF 中重新加载绑定(bind)到 Datagrid 的 DataTable

c# - ListViewItem MouseDoubleClick MVVM方式

c# - 如何为 ViewModel 和 View 创建模板?

c# - 为什么我不能在这种情况下设置不透明度?