在 WPF 中使用数据绑定的控件上自定义测量/排列算法
我正在为自定义编辑器编写排列/测量算法。编辑器使用自定义 UserControl 派生类作为画布上的控件。这些控件是绑定到我的数据模型对象上的各种属性/列表的数据。自定义控件的测量和排列高度依赖于所绑定的数据(因为数据影响控件的大小),并且还依赖于控件测量和排列的顺序。根据我从 MSDN 上的 WPF 文档中可以确定的情况,数据绑定实际上是在测量和排列控件之后以及控件被标记为已加载之前才执行的。据我所知,这意味着我必须等待所有控件加载后才能实际测量和排列自定义画布。因此,我发现自己必须在画布中设置标志来确定何时可以和不能测量/排列它,并使用此标志强制失效和重画。有没有更好的方法在我不知道的画布上进行定制测量/排列?有没有办法在 WPF 中强制进行早期数据绑定?
I'm writing an arrange / measure algorithm for a custom editor. The editor uses custom UserControl-derived classes as controls on the canvas. These controls are data bound to various properties / lists on my data model objects. The measuring and arranging of the custom controls is highly dependent upon the data being bound (because the data affects the size of the controls) and also dependent on the order in which the controls are measured and arranged. From what I've been able to determine from the WPF documentation on MSDN, Data binding isn't actually performed until after controls are measured and arranged, and immediately before the control is marked as loaded. As far as I can tell, this means that I have to wait for all of my controls to be Loaded before I can actually measure and arrange my custom canvas. As a result, I find myself having to set flags in my canvas to determine when I can and can't measure / arrange it, and using this flags to force invalidation and redraw. Is there a better way for customized measurement / arrangement on a canvas that I'm just not aware of ? Is there anyway to force early data binding in WPF ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果数据绑定属性是您控制的自定义 DependencyProperties,则您可以在 DP 上设置元数据属性以指示它们是 AffectMeasure 和 AffectArrange。
如果这些 DP 的值发生变化,这将告诉 WPF 重新测量和重新排列。
您可以在 FrameworkPropertyMetadata 中注册 DP 时设置此项,请参阅此处:http://msdn.microsoft.com/en-us/library/system.windows.frameworkpropertymetadata.aspx
If the data bound properties are custom DependencyProperties that you control then you can set the Metadata properties on the DP's to indicate that they AffectMeasure and AffectArrange.
This will tell WPF to re-measure and re-arrange if the values of these DP's change
You set this when you register the DP in the FrameworkPropertyMetadata see here: http://msdn.microsoft.com/en-us/library/system.windows.frameworkpropertymetadata.aspx
我必须通过检查 [Measure|Arrange]Override 方法来解决数据绑定限制,以确保在实际测量和排列控件之前所有项目均已加载并填充其 ItemSources 和 DataContexts。如果有人找到更好的方法来做到这一点,他们可以将其发布在这里吗?我们将不胜感激。
I've had to workaround the data binding limitation by doing a check in my [Measure|Arrange]Override methods to ensure that all of the items have been Loaded and have their ItemSources and DataContexts populated prior to actually measuring and arranging the controls. If somebody ever finds a better method of doing this, can they please post it here ? It'd be much appreciated.