Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法将ContentControl转换为此格式_C#_This_Contentcontrol - Fatal编程技术网

C# 有没有办法将ContentControl转换为此格式

C# 有没有办法将ContentControl转换为此格式,c#,this,contentcontrol,C#,This,Contentcontrol,我正在尝试将XAML实现到CS中。它成功了,但只有一件事我无法解决,那就是: 将其转换为ContentControl object parent = this; MessageBoxEx.SetParentWindow(this); 必须设置父窗口,否则我将获得空引用 有人能帮我吗 找到了解决办法 public void InitMessageBox() { // Create the ElementHost control fo

我正在尝试将XAML实现到CS中。它成功了,但只有一件事我无法解决,那就是: 将其转换为ContentControl

            object parent = this;
        MessageBoxEx.SetParentWindow(this);
必须设置父窗口,否则我将获得空引用

有人能帮我吗

找到了解决办法

public void InitMessageBox()
    {
        // Create the ElementHost control for hosting the
        // WPF UserControl.
        ContentControl host = new ContentControl();

        host.DataContext = this;
        //host.Dock = DockStyle.Fill;

        // Create the WPF UserControl.
        MessageBoxEx uc =
            new MessageBoxEx();

        // Assign the WPF UserControl to the ElementHost control's
        // Child property.
        //host.Parent = this;
        //host.Child = uc;

        // Add the ElementHost control to the form's
        // collection of child controls.
        //this.Controls.Add(host);

        // p = (ContentControl) this;
        MessageBoxEx.SetParentWindow(host);
        MessageBoxEx.SetMessageForeground(Colors.White);
        MessageBoxEx.SetMessageBackground(Colors.Black);
        MessageBoxEx.SetButtonBackground(MessageBoxEx.ColorFromString("#333333"));
        MessageBoxEx.SetButtonTemplateName("AefCustomButton");
        MessageBoxEx.SetMaxFormWidth(600);
        MessageBoxEx.SetErrorDelegate(new ErrorMsgDelegate());
        // if you want to make the MessageBoxEx silent when you use icons, uncomment the next line
        //MessageBoxEx.SetAsSilent(true);
    }

我没有这样做,但您必须遵循一个特殊的过程,在Windows窗体中使用WPF控件。请查看此链接:

我找到了一些代码:

        public void InitMessageBox()
    {
        // Create the ElementHost control for hosting the
        // WPF UserControl.
        ElementHost host = new ElementHost();
        host.Dock = DockStyle.Fill;

        // Create the WPF UserControl.
        MsgBoxEx.MessageBoxEx uc =
            new MsgBoxEx.MessageBoxEx();

        // Assign the WPF UserControl to the ElementHost control's
        // Child property.
        host.Parent = this;
        host.Child = uc; // Error

        // Add the ElementHost control to the form's
        // collection of child controls.
        this.Controls.Add(host); // host = error

        // p = (ContentControl) this;
        MessageBoxEx.SetParentWindow(uc);
        MessageBoxEx.SetMessageForeground(Colors.White);
        MessageBoxEx.SetMessageBackground(Colors.Black);
        MessageBoxEx.SetButtonBackground(MessageBoxEx.ColorFromString("#333333"));
        MessageBoxEx.SetButtonTemplateName("AefCustomButton");
        MessageBoxEx.SetMaxFormWidth(600);
        MessageBoxEx.SetErrorDelegate(new ErrorMsgDelegate());
        // if you want to make the MessageBoxEx silent when you use icons, uncomment the next line
        //MessageBoxEx.SetAsSilent(true);
    }

这段代码是我从互联网上得到的,但不起作用

我想我需要更多的代码。
是否源自
窗口
?您将
parent
设置为
this
-为什么?你看过这个例子吗?我看过这个例子,它需要类中的窗口,但是如果我从窗口派生,表单设计器就不再工作了。父对象在方法中是this,并尝试通过转换将ContentControl设置为ContentControl父对象=(ContentControl)this;但要有一个合法的演员阵容。是否有方法将此转换为contentcontrol。您正在创建WPF应用程序还是Windows窗体应用程序?Windows窗体应用程序。XAML源代码来自codeproject,位于消息中的链接中。我必须看看如何在这里发布更多的代码行。嗨@Jeff,我不知道如何在注释中发布代码。也许可以回答您的问题?请编辑您的问题,而不是编写答案。很抱歉,不熟悉堆栈溢出