应用 ControlTemplate 后强制绑定发生?
嘿,我正在构建一个 CustomControl,它具有一堆依赖属性和一些视觉部分,例如 Canvas、TextBox 等。 问题是,在为依赖属性分配值时,我需要对视觉元素执行某些操作,即我需要将新的依赖属性的值添加到画布中,但是当绑定启动并分配值时, ControlTemplate 尚未应用,因此崩溃。
解决这个问题的好方法是什么?
谢谢。
Hey, I am building a CustomControl that has a bunch of dependency properties and a few visual parts like, for example, a Canvas, a TextBox, etc.
Problem is, upon assigning a value to the dependency properties, I need to perform certain actions on the visual elements, namely I need to add the new dependency property's value to the Canvas, but when the binding kicks in and the value is assigned, the ControlTemplate has not yet been applied and so it crashes.
What is a good way to fix this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的解决方案是在依赖项属性处理程序中进行空检查以防止崩溃,并重写您知道已构造可视化树的相关方法(最有可能是 OnApplyTemplate),并在那里执行相同的操作来初始化您的视觉效果。
Simple solution is to do a null check in the dependency property handler to prevent crash, and also override the relevant method in which you know that the visual tree is constructed (OnApplyTemplate most likely) and do the same thing there to initialise your visuals.