我有一个未完全显示的控件(通过减小窗口大小)。
但该控件的 ActualWidth 和 RenderSize/DesiredSize 仍然显示其总大小。
我编写了下一个代码,但它忽略了窗口的滚动条宽度,看起来很丑。
也许有一种方法可以以更优雅的方式获得可见的控件大小?
private static double GetVisibleWidth(UIElement element, FrameworkElement container)
{
var visibleBounds =
element.TransformToAncestor(container)
.TransformBounds(new Rect(new Point(0, 0), element.RenderSize));
double visibleWidth = element.RenderSize.Width;
if (container != null)
{
visibleWidth = Math.Min(container.ActualWidth - visibleBounds.X, visibleWidth);
}
return visibleWidth;
}
LayoutInformation.GetLayoutClip https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.layoutinformation.getlayoutclip(v=vs.110).aspx可能就是你所需要的。
返回表示元素可见区域的几何图形。
但请注意,如果element.IsArrangeValid && element.IsMeasureValid == false
,这将返回null
。您可以使用以下方法:
private Size GetVisibleSize(FrameworkElement element)
{
Rect? bounds = LayoutInformation.GetLayoutClip(element)?.Bounds;
if (bounds == null)
{
return new Size(element.ActualWidth, element.ActualHeight);
}
return new Size(bounds.Value.Width, bounds.Value.Height);
}
但是,当您的元素位于滚动查看器内时,这将不起作用,因为它在技术上没有被剪切。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)