开发者

How can I dynamically set the event handler for a TabItem when it is selected?

开发者 https://www.devze.com 2022-12-28 05:29 出处:网络
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 dyna

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;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号