WPF:应用转换后我可以获取 UIElement 的大小吗?
在 WPF/Silverlight 中,我可以在应用转换后获取 UIElement
的计算值吗?
(根据下面的评论):
我有一个堆栈面板,并对其应用了 TranformGroup
。 该组中有两个平移和一个缩放变换。
(警告,前面的伪代码)
groupTransform.children.add(new TranslateTransform());
groupTransform.children.add(new ScaleTransform());
groupTransform.children.add(new TranslateTransform());
containerToScale.RenderTransform = groupTransform;
...
// code that sets values to all the transforms
显然比例变换是我最感兴趣的。
In WPF/Silverlight, can I get the calculated value of a UIElement
after a transformation is applied?
(Per the comment below):
I've got a stack panel and I've applied a TranformGroup
to it. There are two translate and one scale transforms in this group.
(warning, psuedo code ahead)
groupTransform.children.add(new TranslateTransform());
groupTransform.children.add(new ScaleTransform());
groupTransform.children.add(new TranslateTransform());
containerToScale.RenderTransform = groupTransform;
...
// code that sets values to all the transforms
Obviously the scale transform is the one I'm most interested in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
马修,正如您所指出的,如果您应用变换,
ActualWidth
和ActualHeight
不会改变。ActualWidth
和ActualHeight
表示布局系统完成计算控件大小后计算出的宽度/高度(基于诸如Margin
等值,HorizontalAlignment
等)考虑已应用于控件的所有比例变换来获取控件大小的一种方法是沿着可视化树向上并将所有比例变换应用于 <控件的 code>ActualWidth 和
ActualHeight
:请记住,如果您的可视化树很深或者执行多次,这可能会效率低下。 然而,在实践中我从未遇到过任何问题。
Matthew, as you pointed out
ActualWidth
andActualHeight
don't change if you apply transforms.ActualWidth
andActualHeight
represent the calculated width/height after the layout system has finished calculating the size of a control (based on values such asMargin
,HorizontalAlignment
, etc.)One way to get the size of a control taking into account all of the scale transformations that have been applied to it is to walk up the visual tree and apply all scale transforms to the
ActualWidth
andActualHeight
of a control:Keep in mind that this might be inefficient if your visual tree is deep or you perform this many times. In practice I've never run into any problems with this, however.
您可以简单地应用变换:
control.LayoutTransform.Transform(new Point(0,0))
you could simply apply the transform:
control.LayoutTransform.Transform(new Point(0,0))
是的,您可以检查任何
FrameworkElement
上的ActualWidth
和ActualHeight
来弄清楚这一点。 如果您有隐藏的元素,您需要检查它是否可见。如果您想知道控件加载后的情况,请使用
Loaded
事件。Yes, you can check
ActualWidth
andActualHeight
on anyFrameworkElement
to figure this out. You'll need to check whether it is visible though if you have elements that hide.If you want to know after the control has loaded use the
Loaded
event.