我有一个小的WPF控件,它有一个TextWrapping设置为Wrap的TextBlock。我正在尝试将其托管在现有的WinForms应用程序中。我已经将ElementHost停靠在表单的顶部,我想根据ElementHost所需的高度来调整TextBlock的高度。有什么方法可以做到这一点吗?
发布于 2011-02-26 04:25:04
WinForms的大小调整机制与WPF不同。
您是否尝试过将ElementHost的AutoSize属性设置为true?
发布于 2015-01-20 00:05:41
我找到了答案here
这是上面链接中的代码:
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);
} https://stackoverflow.com/questions/5121686
复制相似问题