WPF DesiredSize & RenderSize

摘要:
RenderSizeRenderSize是布局结束后元素的大小,与ActualHeight、ActualWidth类似。所以,WPF布局时,先计算子元素的大小,然后再得到最终渲染后的父元素大小。可以在OnRender和GetLayoutClip方法执行后,获取下最新的RenderSize,确认是否预期结果。

DesiredSize

DesiredSize介绍

关于DesiredSize的介绍,可以查看最新微软文档对DesiredSize的介绍

DesiredSize,指的是元素在布局过程中计算所需要的大小。

通过调用方法Measure计算得到DesiredSize

1 element.Measure(availableSize); 
2   var desiredSize = element.DesiredSize;

DesiredSize的几个概念:

  • DesiredSize是布局过程中使用的
  • DesiredSize是一个只读属性
  • 除了宽高(Width/Height,Max/MinWidth/Height)会影响DesiredSize的值,margin也会直接影响DesiredSize

DesiredSize的宽高

如下是对DesiredSize的宽高测试Demo:

1 <Window x:Class="DesiredSizeTest.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 xmlns:local="clr-namespace:DesiredSizeTest"
7 mc:Ignorable="d"
8 Title="MainWindow"Height="300"Width="500">
9     <Grid>
10         <Button x:Name="TestButton"Width="100"Height="30"Margin="10,2"Padding="2,1"VerticalAlignment="Top"Content="测试DesiredSize"Click="TestButton_OnClick"></Button>
11     </Grid>
12 </Window>

WPF DesiredSize &amp; RenderSize第1张

根据如上Demo,可以得知此Demo中,

DesiredSize.Width=Width+水平方向Margin值,

DesiredSize.Height=Height+竖直方向Margin值

所以如果只是获取控件的实际宽高,不应该通过DesiredSize.Width/Height获取。

RenderSize

RenderSize是布局结束后元素的大小,与ActualHeight、ActualWidth类似。

所以,WPF布局时,先计算子元素的大小(DesiredSize),然后再得到最终渲染后的父元素大小(RenderSize)。

可以在OnRender和GetLayoutClip方法执行后,获取下最新的RenderSize,确认是否预期结果。

关于RenderSize的大小变化,可以监听事件OnRenderSizeChanged

protectedinternalvirtualvoid OnRenderSizeChanged (System.Windows.SizeChangedInfo info);

免责声明:文章转载自《WPF DesiredSize &amp;amp; RenderSize》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ICP算法使用遇到的问题Scrapy进阶知识点总结(二)——选择器Selectors下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF combobox

先写一个数据类Grade.cs usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Collections.ObjectModel; namespace...

WPF 自定义属性

  做了一个自定义控件和一个自定义Grid,里面的元素可以随着绑定属性变化: 效果图(一定滑块): 关键代码: 1、自定义属性代码: public class MyGrid : Grid { public static readonly DependencyProperty ColumnCountProperty = De...

WPF ClickOnce应用程序IIS部署发布攻略

WPF程序非常适合公司内网使用,唯一缺点就是客户端要安装.net框架4.0。优势也很明显,在客户端运行的是一个WinForm程序,自动下载,可以充分利用客户机的性能,而且是以当前的Windows用户权限运行,避免了权限带来的问题。而我觉得最大的益处还是可以自动更新!这样就具备了CS程序功能强大速度快的优点,又有BS程序部署升级容易的优点。 本文要讨论的就是...

WPF之Binding

Bingding是什么 WPF中的Binding注重表达的是一种像桥梁一样的关系,它的两端分别是Binding的源(Source)和目标(Target)。数据从哪里来哪里就是源,Binding是架在中间的桥梁,Binding的目标是数据要往哪去儿。一般情况下,Binding源是逻辑层的对象,Binging的目标是UI层的控件对象。 Binding如何传递数...

WPF 2D绘图(2)Geometry

Shape是对Geometry的一种封装,Shape本质上还是通过绘制Geometry的形状,然后以填充笔刷来呈现效果 如Rectangle <Path Stroke="Black" StrokeThickness="1" Fill="#CCCCFF"> <Path.Data>...

WebLesson04盒子模型,浮动+定位 17王晶龙

一、盒子模型   盒子模型是由内容(content)、边框(border)、外边距(margin)、内边距(padding)组成   1.border:边框宽度,颜色,样式     border-width:     border-color:     border-style:  solid实线 dashed虚线 dotted点线 double双边框bo...