简体   繁体   English

Silverlight / C#画布,他的UIElements和Width

[英]Silverlight/C# Canvas, his UIElements and Width

i created a class "Component", which will be added to the children of my canvas. 我创建了一个“组件”类,它将被添加到画布的子代中。 After the adding, i want to position these components using for example: 添加之后,我想使用例如以下位置放置这些组件:

comp.setValue(Canvas.LeftProperty, 100d);

which works fine. 效果很好。 After that the component will be added to canvas. 之后,该组件将添加到画布。 Now my actual problem is to get the components actual width. 现在我的实际问题是获取组件的实际宽度。 I tried these things like 我尝试过这些事情

comp.Width;
comp.ActualWidth;

but they are 0 or NaN. 但它们是0或NaN。 In my understanding, this should be like that. 以我的理解,应该是这样。 Cause they have been added to the canvas, and should than obtain there specific sizes, or am i wrong with this? 因为它们已经被添加到画布上,并且应该在那里获得特定的尺寸,否则我错了吗?

Also, i added an OnClick-Event on those components, in which they write their ActualWidth on the console, and it works. 另外,我在这些组件上添加了一个OnClick-Event,在其中它们在控制台上写入了ActualWidth,并且可以正常工作。 But i want to get these informations directly after adding them.. 但是我想在添加它们后直接获得这些信息。

Any ideas? 有任何想法吗?

Greetings and stuff! 问候和东西!

The framework will trigger a measure and arrange cycle when you add children to a panel that is included in the visual tree . 当您将子项添加到visual tree包含的面板时,该框架将触发一个measure and arrange cycle After this layout pass, you can get the size by asking ActualWidth and ActualHeight . 通过此布局后,您可以通过询问ActualWidthActualHeight来获取大小。 Apparently the cycle is not completed (or yet to be triggered) when your code runs. 显然,当您的代码运行时,循环尚未完成(或尚未被触发)。 So what can you do? 所以,你可以做什么? You can register an event handler on FrameworkElement.SizeChanged event and run your code inside the event handler. 您可以在FrameworkElement.SizeChanged事件上注册事件处理程序,然后在事件处理程序中运行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM