如何获得XAML的边界动态加载到画布上? [英] How to get bounds of XAML dynamically loaded onto canvas?

查看:82
本文介绍了如何获得XAML的边界动态加载到画布上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将XAML UserControl加载到这样的画布上:

I load a XAML UserControl onto a canvas like this:

var FruitObject = XamlReader.Load(FruitResourceInfo.Stream);
TheFruitFrame.FruitCanvas.Children.Add((UIElement)FruitObject);


我想调整"TheFruitFrame"窗口的大小,使其足够大以显示动态加载的XAML的内容.
但是,我似乎无法访问"FruitObject"的width/height属性(或任何其他属性或其内容).


I''d like to adjust the size of the "TheFruitFrame" window so it''s just big enough to show the contents of the dynamically loaded XAML.
However I can''t seem to access the width/height properties of "FruitObject" (or any other properties or it''s contents).

推荐答案

它"的原因是,在WPF中,只有在加载Window或cotrol或完全调整其大小之前,您才可以访问精确的WidthHeight.

尝试在控件的SizeChanged事件中获取WidthHeight.如果With和Heigh仍然不正确,请尝试ActualWidthActualHeight.
It''s because In WPF you can not access to Exact Width and Height until the Window or the cotrol be loaded or be resized completely.

Try to get Width and Height in SizeChanged event of Controls. If With and Heigh is still incorrect, then try ActualWidth and ActualHeight.


解决方案与设置
一样简单. 相关窗口的
SizeToContent ="WidthAndHeight"
属性,因此它可以自动调整大小,而我不必
手动完成.
The solution turns out to be as simple as setting the
related window''s
SizeToContent="WidthAndHeight"
property so it resizes itself and I don''t have to
do that manually.


这篇关于如何获得XAML的边界动态加载到画布上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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