WPF - 重写我的自定义控件上的 IsVisible
我有一个自定义控件(装饰器),它包装另一个控件(文本框)。
public class MyCustomAdorner : Decorator
{
...
public MyCustomAdorner()
{
Child = new TextBox();
}
}
我希望能够重写 VisibilityChanged ,以便仅当子项的可见性发生变化(而不是实际的装饰器)时才触发 MyCustomAdorner 的事件。我该怎么办呢?
I have a custom control (an Adorner) that wraps another control (a textbox).
public class MyCustomAdorner : Decorator
{
...
public MyCustomAdorner()
{
Child = new TextBox();
}
}
I want to be able to override the VisibilityChanged so that the MyCustomAdorner's event is only fired if the Child's visiblity changes, not the actual decorator. How would I go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一种方法中,我会尝试将装饰器的可见性绑定到文本框的可见性(不确定这个是否有效)。这样,如果文本框改变可见性,装饰器也会随之改变。如果你以两种方式绑定它们,那么它也会以相反的方式工作。因此,如果您不希望它双向工作,请确保不要设置装饰器的可见性。
如果将可见性绑定在一起不起作用,您可以尝试声明一个管理可见性的新属性(例如布尔值),并通过转换器将两个可见性绑定到该布尔值。当您想要更改可见性的某些内容时,可以将此布尔值设置为新值。
覆盖 VisibilityChange 事件对我来说听起来不太好,而且我不确定你是否可以这样做......
On a first approach I would try to bind the Adorner's Visibility to the TextBox's Visibility (not sure if this one works). This way, if the textbox changes visibility, the adorner will follow. If you bind them two way, then it will work the other way around too. So if you don't want it to work both ways, make sure you don't set the Adorner's Visibility.
If binding the Visibilities together doesn't work, you can try to declare a new property (for example a bool), that manages the visibilities, and bind the two Visibilities to that bool through a Converter. And when you want to change something around the Visibilities, you set this bool to a new value.
Overriding the VisibilityChange event doesn't sound good to me, also I'm not sure if you can even do that...