Black background before loading a wpf controll when using ElementHost

前端 未结 3 1386
迷失自我
迷失自我 2020-12-10 23:50

I\'m using WPF in WinForms with ElementHost. When the form loads, there is a flash of black background where the ElementHost is about to load. This looks kind of bad. Any su

3条回答
  •  [愿得一人]
    2020-12-11 00:01

    I know this has already been answered and the question is old but none of the presented answers worked for myself and after a long time of troubleshooting the issue. I finally found an easier answer.

    If you build a class extending from Element Host and in the initial constructor. You can set a Load Event for the Host Container. The Host Container is the panel that the Element Hosts Child is being displayed on top of. From there, just set the Host Containers background color to being of the Element Hosts Parents background color.

    Like this

        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));
            }
        }
    

提交回复
热议问题