3

I have a tab control

<TabControl Height="Auto" Grid.Row="1" ItemsSource="{Binding Tabs}" IsSynchronizedWithCurrentItem="True">

That is bound to Tabs in the ViewModel. I also used CollectionViewSource to focus tabs

protected ObservableCollection<TabViewModel> _tabs;
protected ICollectionView _tabsViewSource;

public ObservableCollection<TabViewModel> Tabs
{
    get { return _tabs; }
}
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); // focus newly created tab
        }
    if (e.OldItems != null && e.OldItems.Count > 0)
        foreach (TabViewModel tab in e.OldItems)
            tab.CloseRequested -= OnCloseRequested;
}

When I have more that 1 tab, when I create new Tabs, tabs are focused properly

alt text

when there are no tabs, new tabs don't seem to be focused properly. notice the tab header

alt text

how might I fix this? or what is causing this behavior? the text box (tab content) is shown but the header don't render like its selected

UPDATE

It works with a fresh file/project ... hmm ... must be some related code ... I might redo that part ...

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • Can you tell us how far the code goes when there are no tabs, does it hit the focus line? – kyndigs Oct 21 '10 at 09:49
  • @kyndigs, what do you mean by how far the code goes? do you mean `_tabsViewSource.MoveCurrentTo(tab)`. That code only gets run when new tabs are created to focus them? or thats what they should do. – Jiew Meng Oct 21 '10 at 10:21

2 Answers2

1

IsSynchronizedWithCurrentItem="True" has no meaning unless you bind your TabControl.ItemsSource to an ICollectionView.

I can't tell if changing your binding from ObservableCollection to ICollectionView will solve your problem, but that is how I have setup my databound tabcontrol.

An alternative could be to expose a new property

public TabViewModel CurrentTabViewModel
{
    get
    {
        return _tabs.CurrentItem as TabViewModel:
    }
    set
    {
        _tabs.MoveCurrentTo(value);
    }
}

and bind TabControl's SelectedItem to CurrentTabViewModel

<TabControl SelectedItem="{Binding Path=CurrentTabViewModel}" ... />
ThomasAndersson
  • 1,844
  • 12
  • 23
  • I tried binding to the `ObservableCollection` and `ICollectionView` but both gave the same result, I also tried binding to `CurrentTabViewModel` and it still failed. any idea whats with that rendering? the text box shows correctly but not the header – Jiew Meng Oct 21 '10 at 22:38
  • Any idea if its just a rendering bug? it only occurs at the start or when the tab control has no tabs and i add 1 – Jiew Meng Oct 21 '10 at 22:43
0

Without the code, that initializes the single-tab-collection, it's just guessing. A Workaround for you would be setting SelectedIndex of the tabView = 0 -> first tab is selected initially.

<TabControl Height="Auto" 
  Grid.Row="1" 
  ItemsSource="{Binding Tabs}" 
  IsSynchronizedWithCurrentItem="True" 
  SelectedIndex="0">
Simon D.
  • 4,451
  • 2
  • 28
  • 57
  • This seem to work only at the start, when I close all tabs and add one, i get the same problem. subsequent tabs rendered correctly – Jiew Meng Oct 21 '10 at 22:39