views:

61

answers:

2

Hey. I have a tabcontrol that is bound to an observable collection.

I've tried doing

var tabitem = (TabItem)this.SingleOrDefault(ti => ti.Name == tabname);
    tabitem.Focus();
    ((UserControl)tabitem.Content).Focus();

And it does seem like it focuses on the tabitem, but only on a btn in the header of the tabitem, not on the content. the tabitem content is another usercontrol.

How can I change the focus to the content, so that the tab actually is selected and not just the tabheader

I know of tabcontrol.selecteditem, but Iøm not really sure how I would implement this as the observablecollection is actually a class that i've called ObservableTabCollection, that just implements observable collection.

A: 

Bind TabControl.SelectedItem two-way to a specific TabItem from your ObservableTabCollection. Then you can set the binding to any item you pull out of your ObservableTabCollection to make that tab the shown tab.

Stephan
Hi Stephan - thanks for your answer. Can you show some example code of that? Would it be in the xaml or in the code-behind? I would prefer it in the code-behind
Jakob
Why do you prefer code-behind? One of the biggest reasons to use Silverlight is to avoid having to use code-behind as much as possible. Also I think maybe I'm a little confused as to how your app is setup. Where is the TabControl getting its TabItem's from? Is the binding to some other control or is it bound to a property from the code-behind?
Stephan
A: 

My solution became to simply create a method on the observabletabcollection that would associate the tabcontrol with the observabletabcollection

public TabControl AssociatedTabControl;

internal void BindToTabControl(TabControl TabCtrl)
        {
            AssociatedTabControl = TabCtrl;
        }

and then inside a method call

if (AssociatedTabControl != null) AssociatedTabControl.SelectedItem = tabitem;
Jakob