tags:

views:

82

answers:

1

In XAML you can do

<TabItem Selector.Selected="myEvenHandler"></TabItem>

to set the event handler for when that tab is selected. How can I do the exact same thing dynamically. I would prefer not to use the SelectionChanged event of TabControl if I can help it. Clearly there is a Selected event on the TabItem I just cannot seem to get at it in code. Here's what I'd like to do.

TabItem item = new TabItem();
MyCustomControl mcc = new MyCustomControl();
item.Content = mcc;
item.Selected += (s,e) =>  // This event does not exist
{
    selectedControl = mcc;
}
myTabControl.Items.Add(item);
+1  A: 

According to the docs for the Selector.Selected attached event, in the "C# Syntax" section:

See AddSelectedHandler, RemoveSelectedHandler

Their page doesn't actually have hyperlinks to the AddSelectedHandler and RemoveSelectedHandler pages, but they're where you want to look. So your code would look something like:

Selector.AddSelectedHandler(item, (s,e) =>
{
    selectedControl = mcc;
});
Joe White
+1 Yeah, looks like I was missing using System.Windows.Controls.Primitives;
juharr