我在带有 ElementHost 的 WinForms 中使用 WPF。当表单加载时,ElementHost 即将加载的地方会出现黑色背景。这看起来有点糟糕。关于如何摆脱这种情况的任何建议?
问问题
4854 次
3 回答
7
隐藏元素 (Visibility = Hidden) 直到 WinForms 控件完全加载...
于 2010-01-18T16:15:07.607 回答
1
我知道这已经得到了回答,而且问题已经过时了,但是在对问题进行了很长时间的故障排除之后,所提供的答案都没有对我有用。我终于找到了一个更简单的答案。
如果您在初始构造函数中构建从 Element Host 扩展的类。您可以为主机容器设置加载事件。Host Container 是 Element Hosts Child 显示在上面的面板。从那里,只需将 Host Containers 背景颜色设置为 Element Hosts Parents 背景颜色。
像这样
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
public class MyElementHost : ElementHost
{
public MyElementHost()
{
this.HostContainer.Loaded += new RoutedEventHandler(HostPanelLoad);
}
public void HostPanelLoad(object sender, RoutedEventArgs e)
{
System.Drawing.Color parentColor = this.Parent.BackColor;
this.HostContainer.Background = new SolidColorBrush(Color.FromArgb(parentColor.A, parentColor.R, parentColor.G, parentColor.B));
}
}
于 2019-03-15T15:34:18.223 回答
0
您需要第一次显示具有空边界的控件以避免黑色闪烁
if (!_control.Created && _control.BackColor != Color.Transparent)
{
_control.Bounds = Rectangle.Empty;
_control.Show();
}
// set control bounds and show it
Rectangle bounds = GetBounds(context, rect);
if (_control.Bounds != bounds)
_control.Bounds = bounds;
if (!_control.Visible)
_control.Show();
于 2010-02-23T19:39:07.317 回答