为什么 TabControl.SelectedContent != (TabControl.SelectedItem as TabItem).Content? [英] Why TabControl.SelectedContent != (TabControl.SelectedItem as TabItem).Content?

查看:29
本文介绍了为什么 TabControl.SelectedContent != (TabControl.SelectedItem as TabItem).Content?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例不应该发出哔哔声(在我看来),但确实发出了哔哔声.为什么?这是否意味着 SelectedContent 属性没有用?是 WPF 中的错误吗?

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

推荐答案

我做了一些测试,发现 SelectedContent 属性在之后设置了 SelectionChanged 事件,而 SelectedItem 设置了 before 事件被引发.!

将其中一个网格更改为 Stackpanel 并在 if 子句上设置断点...

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...

这篇关于为什么 TabControl.SelectedContent != (TabControl.SelectedItem as TabItem).Content?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆