选择两个控件的最大宽度并将其分配给 Silverlight 中的两个控件
我有 2 个控件,每个控件都有动态内容,因此它们的宽度设置为自动。 但我想对齐它们,并且需要以某种方式链接它们的宽度。
例如,Control_1 的宽度为 auto。 Loaded 事件后,其大小变为 200。另一个 Cotnrol_2 变为 250。我需要首先控制将其宽度调整为 250。
任何想法都值得赞赏。
I have 2 controls, each of them has dynamic content, so their width is set to auto.
But I want to align them and I need to link their widths in some way.
So for ex, Control_1 has width auto. After Loaded event its size become 200. Another Cotnrol_2 becomes 250. And I need first control to adjust its width to 250.
Any ideas are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不将宽度属性双向绑定到 ViewModel 的属性。这样,如果一个宽度属性发生变化,根据其值,您可以更改另一个属性的宽度,并且它将反映在控件中。
Why don't you two-way bind the width properties to properties of ViewModel. This way if one width property changes, depending on its value you can change the width of the other property and it will be reflected in the control.
我会通过处理 SizeChanged 事件。我将两个控件上的此事件连接到事件处理程序,如下所示:
我尚未测试此代码,因此我不知道是否将控件的
Width
设置为它的宽度已经存在将导致另一个 SizeChanged 事件触发,再次调用此事件处理程序,从而导致无限递归。因此,如果控件的宽度当前小于maxWidth
,则最好仅将其设置为maxWidth
。我认为使用绑定的方法在这里效果不是特别好。
ActualWidth< FrameworkElement 的 /code>
属性返回呈现控件的实际宽度,但您无法绑定到
ActualWidth
,因为它不是依赖属性。I would do this kind of thing by handling the SizeChanged event on your two controls. I'd wire this event on both controls to an event handler such as the following:
I haven't tested this code, so I don't know whether setting the
Width
of a control to the width it already has will cause another SizeChanged event to fire, invoke this event handler again and hence cause infinite recursion. It might therefore be an idea to only set the control's width tomaxWidth
if it's currently less thanmaxWidth
.I don't think an approach using binding works particularly well here. The
ActualWidth
property of a FrameworkElement returns the actual width that the control has been rendered with, but you can't bind toActualWidth
because it isn't a dependency property.