在 Scrollviewer 中获取儿童可用的宽度 [英] Get Width Available for Children in a Scrollviewer

查看:44
本文介绍了在 Scrollviewer 中获取儿童可用的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取 XAML 中滚动查看器的子项可用的宽度?谢谢.

How do I get the width available for the children of a scroll viewer in XAML? Thanks.

推荐答案

据我所知,没有直接的方法可以做到这一点,因为 WPF 会自动将可用空间传递给子控件的 Measure() 函数,以便它们调整大小以适应可用空间.

There's no direct way of doing this that I know of, since WPF automatically passes the available space in to the child controls' Measure() function so that they size to fit the available space.

请注意,默认情况下,它在垂直方向上传入无穷大,因为内容可以永远垂直滚动.您可以在垂直和水平方向更改滚动条的可见性,以影响是垂直、水平还是同时传递无穷大.

Note that, by default, it passes in infinity for the vertical direction, since content can scroll forever vertically. You can change the visibility of the scroll bars in both the vertical and horizontal direction to affect whether infinity is passed vertically, horizontally, or both.

确定子控件在纯 XAML 中实际布局的宽度的最佳方法是创建一个空控件 - 例如,一个空网格 - 然后绑定到其 ActualWidth 属性:

The best way of figuring out how wide the child controls actually have to layout in pure XAML would be to create an empty control - for instance, an empty grid - and then bind to its ActualWidth property:

<ScrollViewer>
    <StackPanel>
        <Grid x:Name="MeasureGrid"/>
        <TextBox Text="{Binding ElementName=MeasureGrid, Path=ActualWidth}"/>
    </StackPanel>
</ScrollViewer>

除了显示控件实际可用的宽度之外,我在 XAML 中没有看到此信息的任何其他用途 - 我能想到的所有其他场景都可以隐式使用此信息.您能否向我们提供更多有关您正在努力实现的目标的信息?

Aside from displaying the width that is actually available to controls, I don't see any other use for this information in XAML, though - all of the other scenarios I can think of can use this information implicitly. Can you give us more information on what you are trying to accomplish?

这篇关于在 Scrollviewer 中获取儿童可用的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆