locked
Binding a TabControl to an ObservableCollection (MVVM) RRS feed

  • Question

  • I'd developing a MVVM app and I've noticed that when binding a TabControl to an Observable collection the control's SelectedIndex doesn't seem to bind properly to the collections current item. The code I'm using is fairly straightforward:

     

    public ObservableCollection<MyClass> MyCollection {get; set;}
    ...
    MyClass newItem = new MyClass{ ... };
    this.MyCollection.Add(newItem);
    ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.MyCollection);
    if (collectionView != null)
    	collectionView.MoveCurrentTo(newItem);
    
    

     

    The tab gets added to the control but its SelectedIndex doesn't get updated accordingly. I can work around it by creating a wrapper in the control's data context and binding the the SelectedIndex member to that instead:

     

    public int SelectedIndex
    {
    	get {
    		ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.MyCollection);
    		return collectionView.CurrentPosition;
    	}
    	set {
    		ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.MyCollection);
    		collectionView.MoveCurrentToPosition(value);
    		OnPropertyChanged("SelectedIndex");
    	}
    }
    ...
    this.SelectedIndex = this.MyData.IndexOf(newItem);
    
    

    Seems a bit messy though. Is this a bug in WPF? Or is there a way I can bind the control's SelectedIndex to the collection's CurrentPosition directly in Xaml? (I'm currently only binding the ItemsSource.)

     

    Friday, January 21, 2011 10:02 PM

Answers

All replies

  • Hi,

    try setting IsSynchronisedWithCurrentItem="true" on the TabControl.


    http://wpfglue.wordpress.com
    • Marked as answer by Myndale Sunday, January 23, 2011 12:06 PM
    Saturday, January 22, 2011 12:13 PM
  • Maybe it's off-topic, but I still want to mention this. It's not really MVVM to manage your views from your view model. The relation should be this:

    View -> ViewModel -> Model

    Instead of

    View <-> ViewModel <-> Model

     

    Think of MVVM as n-tier architecture. You can always go one layer down, but never a layer up.

     


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for an MVVM framework for WPF and Silverlight? Check out Catel!
    Saturday, January 22, 2011 12:53 PM
  • Hbarck, thanks for the tip but IsSynchronizedWithCurrentItem is for ListBox and ComboBox. It doesn't exist on TabControl :(
    Saturday, January 22, 2011 11:13 PM
  • Geert: good point, but I don't think I'm doing that, am I? The ObservableCollection is in my ViewModel and the "View" that I'm working with here is its CollectionViewSource. This was taken directly from Josh Smith's MSDN article:

    http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

    The MVVM view is a layer above and binds to MyCollection the usual way, so the view model layer isn't directly managing the view layer in any way.

     

    Saturday, January 22, 2011 11:28 PM
  • Hi,

    well, TabControl inherits from Selector, too... just check it out in the object browser...


    http://wpfglue.wordpress.com
    Sunday, January 23, 2011 9:21 AM
  • Hi,

    well, TabControl inherits from Selector, too... just check it out in the object browser...


    http://wpfglue.wordpress.com

    Doh! You're absolutely correct. I must have been relying on Intellisense or something the first time I tried it, I'm old enough to know better :D

    All working fine now, thanks a bunch!

     

    Sunday, January 23, 2011 12:05 PM
  • The article keeps a reference to other view models (and not a view) from another view model. It does not hold references to the view, since that is not MVVM. The bindings of WPF take care of the View -> ViewModel communication. There is nothing you need to do.
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for an MVVM framework for WPF and Silverlight? Check out Catel!
    Monday, January 24, 2011 10:43 AM