Resize Form based on ElementHost size
-
22-06-2021 - |
سؤال
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,
المحلول
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.
نصائح أخرى
sory, but isn't it possible to subscribe to size changes and update the host form?
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;
Instead of setting autosize set:
this.elementHost1.Dock = System.Windows.Forms.DockStyle.Fill;