开发者

MVVM Tabs: Focus new tab

I can add & remove tabs similar to the famous MSDN article. Basically a ObservableCollection<TabViewModels>. And I add tabs like _tabs.Add(new TabViewModel()) but the newest tab is not focused. I want to focus it. How do I do it?

1 way to do it

since i have a view source for my observable collection, I can do the below... another option will be @vorrtex method

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 -= O开发者_开发技巧nCloseRequested;
}


Use 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:

<TabControl ItemsSource="{Binding Pages}" SelectedItem="{Binding CurrentPage, Mode=TwoWay}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜