Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The following sample shouldn't beep (in my opinion) but it does. Why? Does that mean the SelectedContent property is useless? Is it a bug in WPF?

<TabControl SelectionChanged="TabControl_SelectionChanged">
    <TabItem Header="Tab 1">
        <Grid/>
    </TabItem>
    <TabItem Header="Tab 2">
        <Grid/>
    </TabItem>
</TabControl>

 

void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var t = sender as TabControl;
    if (t.SelectedContent != (t.SelectedItem as TabItem).Content) Console.Beep();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

I did some testing and found that the SelectedContent property is set after the SelectionChanged Event has been consumed, while the SelectedItem is set before the event is raised.!

Change one of the Grids to a Stackpanel and set a breakpoint on your if clause...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...