4

I have a Form(winform) and it contains Elementhost. During run time, i am hosting my WPF Usercontrols to this elementhost. I have used AutoSize = True for elementhost.

Elementhost resizes itself basedon WPF Usercontrol size. But How to resize my form based my ElementHost's size.

Thank you,

Harsha
  • 1,861
  • 7
  • 28
  • 56

5 Answers5

2

Here is the answer:

After setting the ElementHost.Child to WPF User control. I will following function:

public System.Windows.Size GetElementPixelSize(UIElement element) 
    { 
        Matrix transformToDevice; 
        var source = PresentationSource.FromVisual(element);
        if (source != null)
            transformToDevice = source.CompositionTarget.TransformToDevice;
        else     
            using (var Hwndsource = new HwndSource(new HwndSourceParameters()))
                transformToDevice = Hwndsource.CompositionTarget.TransformToDevice;


        if (element.DesiredSize == new System.Windows.Size()) 
            element.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity)); 

        return (System.Windows.Size)transformToDevice.Transform((Vector)element.DesiredSize); 
    } 

Original Method posted at: How do I convert a WPF size to physical pixels?

Now I set the client size from new Size.

Community
  • 1
  • 1
Harsha
  • 1,861
  • 7
  • 28
  • 56
  • When you set you set the client size from the new size you mean: ElementHost.ClientSize = GetElementPixelSize(wpfUserControl) ? You are returning System.Windows.Size and ElementHost.ClientSize is of type System.Drawing.Size, so how do you perform such assignment? – Willy Jan 26 '23 at 21:39
0

sory, but isn't it possible to subscribe to size changes and update the host form?

Artiom
  • 7,694
  • 3
  • 38
  • 45
  • Hi, Thanks for the reply. I did the same. I have override the Size MeasureOverride(Size constraint) and I have also triggering an event in this method. My Host Form subscribed to this event and gets notification and update the Form. But I think it is a FIX not a solution. What do you say? – Harsha Jul 20 '12 at 07:11
  • @341184 could you give some code to look. As far for now I don't see prettier solution. – Artiom Jul 20 '12 at 07:23
  • finally I got the answer and I have posted the same. Original Method posted here: http://stackoverflow.com/questions/3286175/how-do-i-convert-a-wpf-size-to-physical-pixels – Harsha Jul 20 '12 at 09:46
0

I have a solution for this, but not sure about output.It worked in WPF Desktop application. what you have to do is set the height and width to NAN (not a number)

this.Width = double.NaN;
this.Height = double.NaN;
Pintu Paul
  • 389
  • 3
  • 18
  • finally I got the answer and I have posted the same. Original Method posted here: http://stackoverflow.com/questions/3286175/how-do-i-convert-a-wpf-size-to-physical-pixels – Harsha Jul 20 '12 at 09:46
0

Instead of setting autosize set:

 this.elementHost1.Dock = System.Windows.Forms.DockStyle.Fill;
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
0

It's an old question, but I come by it often, so it's still relevant, so here is my solution:

What I do is:

a. When I want WPF view to match (dock) window size:

ElementHost eh = new ElementHost();
eh.Dock = System.Windows.Forms.DockStyle.Dock;
eh.AutoSize = true;
eh.Child = wpfView;
eh.CreateControl();
this.Controls.Add(eh);
this.AutoSize = false;

NOTE: You can set eh min size to have scrollbars, if form too small.

b: When I want the host Form to match the WPF View:

ElementHost eh = new ElementHost();
eh.Dock = System.Windows.Forms.DockStyle.None;
eh.AutoSize = true;
eh.Child = wpfView;
eh.CreateControl();
this.Controls.Add(eh);
this.AutoSize = true;
this.AutoSizeMode=AutoSizeMode.GrowAndShrink;