Resize ElementHost element to size of hosted XAML UserControl

I would like to expand the panel in my Windows Forms application. I looked to see if this could be used using the WPF Expander. I created the Xaml UserControl, where I inherited from Expander, not UserControl. I have a trigger on Expander to set its size.

Is it possible to change the height of the ElementHost element to reflect the resizing of the child? Or am I just better off creating an expanding panel in Windows Forms?

I am using C # .Net 3.5.

Greetings

+2
source share
2 answers

Is there any specific reason you inherit from Expander and just use Expander in the user interface?

If you configure the H / V alignment properties for Expander, you can get most of the standard calibration rules without using size triggers. In my experience, part of the contents of an Expander is automatically sized.

If you are trying to completely remove part of the header, you can see how to create your own control template for the expander.

0
source

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

Since you already subclass Expander :

  • Override the MeasureOverride
  • After calculating the measurement, use PresentationSource.From(visual).CompositionTarget.TransformToDevice.Transform(point) to get the device coordinates
  • Update ElementHost.Size .

For this instance of the Expander subclass, this requires a pointer to an ElementHost .

A more general solution would be to create a new class to handle synchronization. This will be a subclass of FrameworkElement and will be a direct descendant of ElementHost .

0
source

All Articles