Resize ElementHost to size of the hosted XAML UserControl

后端 未结 2 865
攒了一身酷
攒了一身酷 2021-02-11 06:01

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 wh

2条回答
  •  长情又很酷
    2021-02-11 07:01

    Yes. You need to override MeasureOverride in your outermost WPF control, convert the size from WPF coordinates to device coordinates, and update ElementHost.Size.

    Since you are already subclassing Expander:

    1. Override the MeasureOverride method
    2. After the measurement is calculated, use PresentationSource.From(visual).CompositionTarget.TransformToDevice.Transform(point) to get the device coordinates
    3. Update ElementHost.Size.

    Your Expander subclass instance will need a pointer to ElementHost to do this.

    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.

提交回复
热议问题