选择两个控件的最大宽度并将其分配给 Silverlight 中的两个控件

发布于 2024-12-22 20:06:40 字数 179 浏览 6 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘逸的'云 2024-12-29 20:06:40

为什么不将宽度属性双向绑定到 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.

时光是把杀猪刀 2024-12-29 20:06:40

我会通过处理 SizeChanged 事件。我将两个控件上的此事件连接到事件处理程序,如下所示:

void ControlAOrB_SizeChanged(object sender, SizeChangedEventArgs e)
{
    double maxWidth = Math.Max(controlA.ActualWidth, controlB.ActualWidth);
    controlA.Width = maxWidth;
    controlB.Width = maxWidth;
}

我尚未测试此代码,因此我不知道是否将控件的 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:

void ControlAOrB_SizeChanged(object sender, SizeChangedEventArgs e)
{
    double maxWidth = Math.Max(controlA.ActualWidth, controlB.ActualWidth);
    controlA.Width = maxWidth;
    controlB.Width = maxWidth;
}

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 to maxWidth if it's currently less than maxWidth.

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 to ActualWidth because it isn't a dependency property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文