我创建了一个类“Component”,它将添加到我的画布的子节点中。 添加后,我想使用例如这些组件定位:
comp.setValue(Canvas.LeftProperty, 100d);哪个工作正常。 之后,组件将添加到画布中。 现在我的实际问题是获得组件的实际宽度。 我试过这些东西
comp.Width; comp.ActualWidth;但他们是0或NaN。 根据我的理解,这应该是这样的。 因为它们已被添加到画布中,并且应该获得特定尺寸,或者我错了吗?
此外,我在这些组件上添加了一个OnClick-Event,它们在控制台上编写了ActualWidth,并且它可以工作。 但我想在添加它们后直接获取这些信息..
有任何想法吗?
问候和东西!
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. 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. But i want to get these informations directly after adding them..
Any ideas?
Greetings and stuff!
最满意答案
将子项添加到visual tree包含的面板时,框架将触发measure and arrange cycle 。 在布局过后,您可以通过询问ActualWidth和ActualHeight来获取大小。 显然,代码运行时循环未完成(或尚未触发)。 所以,你可以做什么? 您可以在FrameworkElement.SizeChanged事件上注册事件处理程序,并在事件处理程序中运行您的代码。
The framework will trigger a measure and arrange cycle when you add children to a panel that is included in the visual tree. After this layout pass, you can get the size by asking ActualWidth and ActualHeight. 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.
更多推荐
添加,added,comp,组件,ActualWidth,电脑培训,计算机培训,IT培训"/> <meta name="
发布评论