How can I dynamically set the event handler for a TabItem when it is selected?
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);
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;
});
精彩评论