wpfwinformselementhost

Resize Form based on ElementHost size


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,


Solution

  • 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.