调整ElementHost的到托管用户控件XAML的大小 [英] Resize ElementHost to size of the hosted XAML UserControl
问题描述
我想在我的Windows展开面板窗体应用程序。我有一个看看,看看这是否会使用WPF扩展控制成为可能。我创建了一个用户控件的XAML,我已经从扩展,而不是用户控件继承。我对扩展器,用于设置它的大小的触发
I would like an expanding panel in my Windows Forms app. I was having a look to see if this would be possible using the WPF Expander control. I've created a Xaml UserControl where I've inherited from Expander rather than UserControl. I have a trigger on the Expander for setting it's size.
是否有可能改变ElementHost的高度,以反映儿童的尺寸的变化?或将我只是会更好的Windows窗体创建一个扩展面板?
Is it possible to change the height of the ElementHost to reflect the change in the size of the child? Or would I just be better off creating an expanding Panel in Windows Forms?
我使用C#.NET 3.5。
I'm using C# .Net 3.5.
干杯
推荐答案
是的。你需要重写你最外层的WPF控件的MeasureOverride
,转换WPF规模坐标到设备坐标,并更新 ElementHost.Size $ C $ 。C>
Yes. You need to override MeasureOverride
in your outermost WPF control, convert the size from WPF coordinates to device coordinates, and update ElementHost.Size
.
既然你已经继承扩展
:
- 重写
的MeasureOverride
法 - 测量计算后,使用
PresentationSource.From(视觉).CompositionTarget.TransformToDevice.Transform(点)
来获取设备坐标 - 更新
ElementHost.Size
。
- Override the
MeasureOverride
method - After the measurement is calculated, use
PresentationSource.From(visual).CompositionTarget.TransformToDevice.Transform(point)
to get the device coordinates - Update
ElementHost.Size
.
您扩展
子类的实例将需要一个指针 ElementHost的
来做到这一点。
Your Expander
subclass instance will need a pointer to ElementHost
to do this.
一个更通用的解决方案是创建一个新的类来处理同步。它将继承 FrameworkElement的
并 ElementHost的
的直接子。
A more general solution would be to create a new class to handle the synchronization. It would subclass FrameworkElement
and be the direct child of ElementHost
.
这篇关于调整ElementHost的到托管用户控件XAML的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!