如何使用 Height=Auto 访问元素的实际高度

发布于 2024-07-26 12:08:17 字数 2074 浏览 6 评论 0原文

我有这样的情况:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="560"/>
        <ColumnDefinition Width="250"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" /> <!-- status infos & content start -->
        <RowDefinition Height="Auto" /> <!-- status infos -->
        <RowDefinition Height="Auto" /> <!-- status infos -->
        <RowDefinition Height="Auto" /> <!-- status infos -->
        <RowDefinition Height="*"/>     <!-- content ends -->
    </Grid.RowDefinitions>

    <!-- image a list of custom controls directed to the first or second column on all rows here -->

    <SomeCustomControl Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Grid.RowSpan="2" />
</Grid>

如您所见,我有 2 列,右侧一列或多或少保留用于状态信息,左侧用于内容。 “SomeCustomControl”包含一个太宽的控件,需要将其设置为 ColumnSpan="2"。 请注意,右栏中仍然有状态控件。 在 SomeCustomControl 中,我有这样的内容:

<Grid x:Name="LayoutRoot">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="250"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        [...]
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="*"  />   <!-- problem control here -->
        <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions>

    <!-- a list of Controls limited to the first column -->

    <ProblemControl Grid.Column="0" Grid.ColumnSpan="2" />
</Grid>

现在,SomeCustomControl 的第一行包含仅限于第一列的控件,然后有一行包含我的 ProblemControl。 状态控件的高度不是预先确定的,而是取决于显示的状态信息。 SomeCustomControl 中仅限于第一列的控件也具有不同的高度,当前通过内容自动确定。

我现在遇到的问题是 ProblemControl 与我的一些状态控件重叠。 我尝试计算状态控件和 SomeCustomControl 中有限控件的高度,但由于所有控件的大小都是动态调整的,我似乎无法获得正确的高度。 RowDefinitions的高度都包含类型为Auto且值为1的高度,具体控件的高度似乎是NaN。

关于如何计算高度或以其他方式防止重叠的任何想法。

I have a situation like this:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="560"/>
        <ColumnDefinition Width="250"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" /> <!-- status infos & content start -->
        <RowDefinition Height="Auto" /> <!-- status infos -->
        <RowDefinition Height="Auto" /> <!-- status infos -->
        <RowDefinition Height="Auto" /> <!-- status infos -->
        <RowDefinition Height="*"/>     <!-- content ends -->
    </Grid.RowDefinitions>

    <!-- image a list of custom controls directed to the first or second column on all rows here -->

    <SomeCustomControl Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Grid.RowSpan="2" />
</Grid>

As you can see I have 2 Columns, the right one is more or less reserved for status information, the left for content. "SomeCustomControl" contains a control so wide it needs to be set to ColumnSpan="2". Notice there are still the status control in the right column. In SomeCustomControl I have something like this:

<Grid x:Name="LayoutRoot">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="250"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        [...]
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="*"  />   <!-- problem control here -->
        <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions>

    <!-- a list of Controls limited to the first column -->

    <ProblemControl Grid.Column="0" Grid.ColumnSpan="2" />
</Grid>

Now, the first Rows of SomeCustomControl contain controls limited to the first column, then there is a row that contains my ProblemControl. The Height of the status controls is not predetermined and depends on the shown status information. The controls in SomeCustomControl that are limited to the first column also have different heights, that are currently determined automatically through the content.

I now have the problem that ProblemControl overlaps with some of my status controls. I tried to calculate the height of my status controls and the limited controls in SomeCustomControl, but as all controls are sized dynamically I can't seem to get correct Heights. The Height of the RowDefinitions all contains Heights of type Auto and value 1, the Heights of the concrete Controls seems to be NaN.

Any ideas as to how I can calc the heights or prevent the overlapings in other ways.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

凤舞天涯 2024-08-02 12:08:17

我遇到了一些同样的问题,但最近找到了解决方案。 无法访问宽度或高度设置为“自动”的控件的宽度和高度属性的原因是运行时系统在设置属性值之前查询它们。 属性 ActualWidthActualHeight 声称获取控件的渲染高度,因此理论上,您认为可以简单地等到 SL 应用程序完成加载,然后执行您的操作查询,因为届时将呈现控件,因此应设置 ActualHeight/ActualWidth 值。

可悲的是,情况也并非如此。 设置这些值时似乎没有任何保证,因此我使用的解决方法是挂钩到我想要其值的控件的 SizeChanged 事件。 每当控件的宽度和高度属性发生更改时,都会触发 SizeChanged,因此如果我处理该事件,我可以保证将这些值设置为 NaN 以外的值。

在该事件的处理程序中执行您需要执行的任何逻辑,您会发现值已设置。

I've encountered somewhat of the same problem, but came across the solution recently. The reason why you can't access the width and height properties of a control with width or height set to Auto is that the run time system is querying for the property values before they've been set. The properties ActualWidth and ActualHeight claim to get the rendered height of controls so in theory, you'd think you could simply wait until the SL application had finished loading and then perform your query, since the controls would be rendered by then and therefore, the ActualHeight/ActualWidth values should be set.

Sadly, this isn't the case either. There doesn't seem to be any guarantee when those values are set, so the workaround I used is to hook into the SizeChanged-event of the control whose values I want. SizeChanged is triggered whenever the width and height properties of a control are changed, so if I handle that event, I am guaranteed that the values are set to something other than NaN.

Do whatever logic you need to perform in a handler for that event, and you'll find the values are set.

泼猴你往哪里跑 2024-08-02 12:08:17

您是否尝试过使用控件上的 ActualHeight 属性?

Have you tried with ActualHeight property on control ?

偏爱你一生 2024-08-02 12:08:17

您应该能够通过正确使用Grid和一些面板(DockPanelStackPanel可能非常有用)来解决这个问题。 您可能还需要在某些控件上将 Horizo​​ntalAlignmentVerticalAlignment 属性设置为 Stretch

您的示例不足以让我们重现您的问题或知道我们已正确解决该问题。 如果您需要更具体的帮助,请展开示例,以便我们可以运行它并查看您的问题。

You ought to be able to solve this with proper use of the Grid and some panels (DockPanel and StackPanel can be quite useful). You may also need the HorizontalAlignment or VerticalAlignment properties set to Stretch on some controls.

Your example doesn't have enough in it for us to duplicate your problem or to know that we've addressed it correctly. If you'd like more specific help, please expand the example so we can run it and see your issue.

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