[英]Resize ElementHost to size of the hosted XAML UserControl
I would like an expanding panel in my Windows Forms app. 我想要Windows窗体应用程序中的扩展面板。 I was having a look to see if this would be possible using the WPF Expander control. 我正在查看使用WPF Expander控件是否可行。 I've created a Xaml UserControl where I've inherited from Expander rather than UserControl. 我创建了一个Xaml UserControl,它继承自Expander而不是UserControl。 I have a trigger on the Expander for setting it's size. 我在扩展器上有一个触发器来设置它的大小。
Is it possible to change the height of the ElementHost to reflect the change in the size of the child? 是否可以更改ElementHost的高度以反映孩子大小的变化? Or would I just be better off creating an expanding Panel in Windows Forms? 还是我最好在Windows窗体中创建一个扩展面板?
I'm using C# .Net 3.5. 我正在使用C#.Net 3.5。
Cheers 干杯
Is there any particular reason you are inheriting from Expander vs. just using an Expander in your UI? 您是从扩展器继承而来的,还是仅在UI中使用扩展器,有什么特殊的原因吗?
If you set up the H/V Alignment properties of the Expander, you should be able to get most standard sizing behaviors without having size triggers. 如果设置扩展器的“水平/垂直对齐”属性,则应该能够获得大多数标准的尺寸调整行为,而无需使用尺寸触发器。 From my experience, the content portion of the Expander automatically sizes to fit. 根据我的经验,扩展器的内容部分会自动调整大小以适应需要。
If you're trying to completely remove the header part, then you might look at making your own ControlTemplate for the Expander. 如果要完全删除标题部分,则可以考虑为扩展器制作自己的ControlTemplate。
Yes. 是。 You need to override MeasureOverride
in your outermost WPF control, convert the size from WPF coordinates to device coordinates, and update ElementHost.Size
. 您需要在最外面的WPF控件中重写MeasureOverride
,将大小从WPF坐标转换为设备坐标,然后更新ElementHost.Size
。
Since you are already subclassing Expander
: 由于您已经继承了Expander
:
MeasureOverride
method 覆盖MeasureOverride
方法 PresentationSource.From(visual).CompositionTarget.TransformToDevice.Transform(point)
to get the device coordinates 计算出度量后,使用PresentationSource.From(visual).CompositionTarget.TransformToDevice.Transform(point)
获取设备坐标 ElementHost.Size
. 更新ElementHost.Size
。 Your Expander
subclass instance will need a pointer to ElementHost
to do this. 您的Expander
子类实例将需要一个指向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
. 它会继承FrameworkElement
并成为ElementHost
的直接子级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.