Reputation: 1879
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,
Upvotes: 4
Views: 4427
Reputation: 1
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;
Upvotes: 0
Reputation: 36048
Instead of setting autosize set:
this.elementHost1.Dock = System.Windows.Forms.DockStyle.Fill;
Upvotes: 0
Reputation: 1879
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.
Upvotes: 2
Reputation: 399
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;
Upvotes: 0
Reputation: 7837
sory, but isn't it possible to subscribe to size changes and update the host form?
Upvotes: 0