I have a small WPF control that has a TextBlock with TextWrapping set to Wrap. I am trying to host this in an existing WinForms application. I have the ElementHost docked to the top of the form, and I would like to size the height of the ElementHost based on the height that the TextBlock require. Is there any way to accomplish this?
Auto size ElementHost to its content
Asked Answered
The resizing mechanism of WinForms is different from WPF's.
Have you tried setting the AutoSize property of the ElementHost to true?
That was my first thought, but it doesn't do anything. –
Wreath
Did you have a look at this post: #1086684 –
Karlene
Setting the
AutoSize
property for the ElementHost
as well as the AutoSize
for the control that contains the ElementHost
, in my case a Form
, works for me. –
Polyneuritis @MikedeKlerk Thanks, that works like a charm, in my case with the
ElementHost
contained within a Panel
–
Artois @MikedeKlerk in my case im setting "AutoSize" to both the "ElementHost" and "Form" Onloading window in first time Form size greater then my control size –
Ulrikaumeko
I found the answer here
this is code from the link above:
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);
}
© 2022 - 2024 — McMap. All rights reserved.