WPF DesiredSize & RenderSize
DesiredSize
DesiredSize介绍
关于DesiredSize的介绍,可以查看最新微软文档对DesiredSize的介绍
DesiredSize,指的是元素在布局过程中计算所需要的大小。
通过调用方法Measure计算得到DesiredSize
element.Measure(availableSize);
var desiredSize = element.DesiredSize;
DesiredSize的几个概念:
- DesiredSize是布局过程中使用的
- DesiredSize是一个只读属性
- 除了宽高(Width/Height,Max/Min Width/Height)会影响DesiredSize的值,margin也会直接影响DesiredSize
DesiredSize的宽高
如下是对DesiredSize的宽高测试Demo:
<Window x:Class="DesiredSizeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DesiredSizeTest"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="500">
<Grid>
<Button x:Name="TestButton" Width="100" Height="30" Margin="10,2" Padding="2,1" VerticalAlignment="Top" Content="测试DesiredSize" Click="TestButton_OnClick"></Button>
</Grid>
</Window>

根据如上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
protected internal virtual void OnRenderSizeChanged (System.Windows.SizeChangedInfo info);
WPF DesiredSize & RenderSize的更多相关文章
- DesiredSize,RenderSize&& Width ,ActualWidth
做UI的时候刚入门,很多属性摸不着头脑,需要的功能和属性不能很快联系联想到,所以要慢慢积累UIElement 的DesiredSize 和 RenderSize UIElement 的DesiredS ...
- WPF快速入门系列(1)——WPF布局概览
一.引言 关于WPF早在一年前就已经看过<深入浅出WPF>这本书,当时看完之后由于没有做笔记,以至于我现在又重新捡起来并记录下学习的过程,本系列将是一个WPF快速入门系列,主要介绍WPF中 ...
- WPF/Silverlight Layout 系统概述——Arrange(转)
Arrange过程概述 普通基类属性对Arrange过程的影响 我们知道Measure过程是在确定DesiredSize的大小,以便Arrange过程参考这个DesiredSize,确定给MyPane ...
- WPF中元素拖拽的两个实例
今天结合之前做过的一些拖拽的例子来对这个方面进行一些总结,这里主要用两个例子来说明在WPF中如何使用拖拽进行操作,元素拖拽是一个常见的操作,第一个拖拽的例子是将ListBox中的子元素拖拽到ListV ...
- WPF Tutorial - Using A Visual Collection
While WPF and XAML make the common 90% of UI programming quite easy, sometimes it gets a little odd ...
- 关于WPF你应该知道的2000件事
原文 关于WPF你应该知道的2000件事 以下列出了迄今为止为WPF博客所知的2,000件事所创建的所有帖子. 帖子总数= 1,201 动画 #7 - 基于属性的动画 #686 - 使用动画制作图像脉 ...
- 【WPF】UI虚拟化之------自定义VirtualizingWrapPanel
原文:[WPF]UI虚拟化之------自定义VirtualizingWrapPanel 前言 前几天QA报了一个关于OOM的bug,在排查的过程中发现,ListBox控件中被塞入了过多的Item,而 ...
- WPF Layout 系统概述——Arrange
原文:WPF Layout 系统概述--Arrange Arrange过程概述 普通基类属性对Arrange过程的影响 我们知道Measure过程是在确定DesiredSize的大小,以便Arrang ...
- [WPF自定义控件库]自定义Expander
1. 前言 上一篇文章介绍了使用Resizer实现Expander简单的动画效果,运行效果也还好,不过只有展开/折叠而缺少了淡入/淡出的动画(毕竟Resizer模仿Expander只是附带的功能).这 ...
随机推荐
- [Codeforces Round #516][Codeforces 1063C/1064E. Dwarves, Hats and Extrasensory Abilities]
题目链接:1063C - Dwarves, Hats and Extrasensory Abilities/1064E - Dwarves, Hats and Extrasensory Abiliti ...
- spring-security权限管理学习目标
1.SVN基本介绍: 1.svn基本的概念 2.svn架构 3.svn下载与安装 4.svn搭建与基本操作 2.svn基本操作 1.操作1 2.操作2 3.冲突产生 4.冲突解决 3.SVN在IDEA ...
- 发现了学校教务处官网的两个BUG
许久没有写博客了,感觉自己技术还差的好多-_-好像没啥好写的,之前学完了某易的WEB安全基础视频教程,自认对WEB安全入了门,忍不住就想拿学校教务处官网来练练手 教务处登录界面如图所示(为保护隐私,部 ...
- python Tags 母板 组件 静态文件相关 自定义simpletag inclusion_tag
一.Tags(一)for 1.基本用法 <ul> {% for user in user_list %} <li>{{ user.name }}</li> {% e ...
- 出现 HTTP Status 500 - Servlet.init() for servlet springmvc threw exception 异常的原因及解决方法
今天做项目的时候遇到了这个问题 其中有一句是Caused by: org.springframework.beans.factory.BeanCreationException: Error crea ...
- 搭建Windows故障转移群集
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/windows 概述 本章内容主要讲述搭建windows故障转移群集 环境: 域服务器:windows server 2008 R ...
- Python 安装和 Pycharm 环境配置
一.Python 简介 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它 ...
- [Swift]LeetCode344. 反转字符串 | Reverse String
Write a function that takes a string as input and returns the string reversed. Example 1: Input: &qu ...
- [Swift]LeetCode814. 二叉树剪枝 | Binary Tree Pruning
We are given the head node root of a binary tree, where additionally every node's value is either a ...
- [Swift]LeetCode863. 二叉树中所有距离为 K 的结点 | All Nodes Distance K in Binary Tree
We are given a binary tree (with root node root), a targetnode, and an integer value K. Return a lis ...