wpf 如何确定用户何时可以看到控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15065758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:42:29  来源:igfitidea点击:

How to figure out when control is visible to user?

wpf

提问by snowy hedgehog

I would like to know how to figure out when a control is visbile to user and when not?

我想知道如何确定控件何时对用户可见,何时不可见?

Let says I am writing a custom control which extends a TextBoxand I would like to know when is visible to do some custom logic inside the handler triggered on visible changed.

假设我正在编写一个扩展 a 的自定义控件TextBox,我想知道什么时候可以在可见更改时触发的处理程序中执行一些自定义逻辑。

How to do so? How to figure out if the control is visible to user or not? The custom control which extends a TextBoxcould be inside a Buttonfor instance and Buttoncould receive Visibility.Collapsedso all its inner nested controls would be invisibe and thats what I want to know. How to figure out if visible no matter what value to Visibility property is set?

怎么做?如何确定控件是否对用户可见?例如,扩展 a 的自定义控件TextBox可以在 a 内部Button,并且Button可以接收,Visibility.Collapsed因此它的所有内部嵌套控件都是不可见的,这就是我想知道的。无论 Visibility 属性设置什么值,如何确定是否可见?

回答by Anatolii Gabuza

You should use UIElement.IsVisibleChangedevent. It occurs when value of the IsVisiblechanges that is read-only dependency property and indicates elements Visibility on UI.
UIElement.LayoutUpdated- event that could be used for recalculating controls visibility.

你应该使用UIElement.IsVisibleChanged事件。当IsVisible只读依赖属性的值发生变化并指示元素在 UI 上的可见性时,就会发生这种情况 。
UIElement.LayoutUpdated- 可用于重新计算控件可见性的事件。

UPDATE.Seems like it is exact duplicate:
In WPF, how can I determine whether a control is visible to the user?

更新。似乎完全重复:
在 WPF 中,如何确定控件是否对用户可见?