[英]MVVM Tabs: Focus new tab
I can add & remove tabs similar to the famous MSDN article . 我可以添加和删除类似于着名的MSDN文章的选项卡。 Basically a
ObservableCollection<TabViewModels>
. 基本上是
ObservableCollection<TabViewModels>
。 And I add tabs like _tabs.Add(new TabViewModel())
but the newest tab is not focused. 我添加了像
_tabs.Add(new TabViewModel())
的标签,但最新的标签没有聚焦。 I want to focus it. 我想集中注意力。 How do I do it?
我该怎么做?
1 way to do it 1种方法
since i have a view source for my observable collection, I can do the below... another option will be @vorrtex method 因为我有一个可观察集合的视图源,我可以在下面做...另一种选择是@vorrtex方法
public void OnTabsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null && e.NewItems.Count > 0)
foreach (TabViewModel tab in e.NewItems)
{
tab.CloseRequested += OnCloseRequested;
_tabsViewSource.MoveCurrentTo(tab);
}
if (e.OldItems != null && e.OldItems.Count > 0)
foreach (TabViewModel tab in e.OldItems)
tab.CloseRequested -= OnCloseRequested;
}
Use SelectedItem: 使用SelectedItem:
public ObservableCollection<TabViewModel> Pages { get; set; }
private TabViewModel currentPage;
public TabViewModel CurrentPage
{
get { return currentPage; }
set
{
currentPage = value;
OnPropertyChanged("CurrentPage");
}
}
public void AddPage()
{
var page = new TabViewModel();
this.Pages.Add(page);
this.CurrentPage = page;
}
XAML: XAML:
<TabControl ItemsSource="{Binding Pages}" SelectedItem="{Binding CurrentPage, Mode=TwoWay}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.