我有一个没有完全显示的控件(通过减小窗口大小).
但是此控件的ActualWidth和RenderSize / DesiredSize仍然显示它的总大小.
我编写了下一个代码,但它忽略了窗口的滚动条宽度并且看起来很难看.
也许有办法以更优雅的方式获得可见的控制尺寸?
但是此控件的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
可能是你需要的.
Returns a Geometry that represents the visible region of an element.
但请注意,如果元素.IsArrangeValid&& element.IsMeasureValid == false,这将返回null.这是您可以使用的方法:
private Size GetVisibleSize(FrameworkElement element) { Rect? bounds = Layout@R_905_404[email protected](element)?.Bounds; if (bounds == null) { return new Size(element.ActualWidth,element.ActualHeight); } return new Size(bounds.Value.Width,bounds.Value.Height); }
当你的元素在scrollviewer中时,这不起作用,因为它在技术上没有被剪裁.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。