为什么 TabControl.SelectedContent != (TabControl.SelectedItem as TabItem).Content?

编程入门 行业动态 更新时间:2024-10-26 06:30:07
本文介绍了为什么 TabControl.SelectedContent != (TabControl.SelectedItem as TabItem).Content?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

以下示例不应该发出哔哔声(在我看来),但确实发出了哔哔声.为什么?这是否意味着 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 11:39:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408860.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SelectedContent   TabControl   SelectedItem   Content   TabItem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!