简体   繁体   English

将ElementHost调整为托管XAML UserControl的大小

[英]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

  1. Override the MeasureOverride method 覆盖MeasureOverride方法
  2. After the measurement is calculated, use PresentationSource.From(visual).CompositionTarget.TransformToDevice.Transform(point) to get the device coordinates 计算出度量后,使用PresentationSource.From(visual).CompositionTarget.TransformToDevice.Transform(point)获取设备坐标
  3. Update 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.

相关问题 如何从WinForms ElementHost中托管的WPF UserControl获取对Window的引用 - How to get a reference to the Window from a WPF UserControl hosted in a WinForms ElementHost 如何在 Winforms 窗体和由 Winforms 窗体中的 elementhost 托管的 WPF 用户控件之间使用委托? - How to use a delegate between a Winforms Form and a WPF Usercontrol hosted by an elementhost in the Winforms form? 如何在XAML中为WPF中托管的Winforms UserControl解析自定义属性名称? - How To Resolve Custom Property Name In XAML For a Winforms UserControl Hosted In WPF? 初始化后,使用其父级调整托管在窗口中的 UserControl - Resize UserControl hosted in a Window with it's parent after initialization WPF DependencyProperty未在WPF UserControl(ElementHost)中更新 - WPF DependencyProperty not updating in WPF UserControl (ElementHost) 在ElementHost中托管的WPF元素未正确重绘 - WPF element hosted in ElementHost not redrawing correctly 托管在ElementHost控件中的WPF控件会更改颜色 - WPF control hosted in an ElementHost control changes colour .NET UserControl:Size属性在Resize事件上给出错误的值 - .NET UserControl: Size property gives incorrect value on Resize event 如何使用KeyPresses在不带窗口的ElementHost中托管WPF用户控件 - How to host a WPF UserControl in ElementHost without Window with KeyPresses 将xaml文件导入到usercontrol - Import xaml file into usercontrol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM