【问题标题】:Is there a way to convert ContentControl to this有没有办法将 ContentControl 转换为此
【发布时间】:2021-01-10 22:19:08
【问题描述】:

MessageBoxEx

我试图在 CS 中实现 XAML。它成功了,但只有一件事我无法解决,那就是: 将此转换为 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);
    }

【问题讨论】:

  • 我认为我需要更多代码。 this 是从 Window 派生的吗?您将parent 设置为this - 为什么?你看过这个例子吗?
  • 我查看了示例,它需要类中的窗口,但如果我从 Windows 派生,表单设计器将不再工作。父级在方法中是 this 并尝试通过强制转换设置 ContentControl 到 ContentControl parent = (ContentControl) this;但是要获得非法演员表。有没有办法将其转换为 contentcontrol。
  • 您是在创建 WPF 应用还是 Windows 窗体应用?
  • 一个 windows 窗体应用程序。 XAML 源来自 codeproject,位于消息中的链接中。我必须看看如何在这里发布更多代码行。

标签: c# this contentcontrol


【解决方案1】:

我没有这样做,但您必须遵循特殊的过程才能将 WPF 控件与 Windows 窗体一起使用。请看这个链接:Use WPF controls in Windows Forms apps

【讨论】:

  • 嗨@Jeff,我不知道如何在 cmets 中发布代码。也许回答你的问题?
【解决方案2】:

我找到了一些代码:

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

此代码我从互联网上获得但不起作用

【讨论】:

  • 请编辑您的问题,而不是写答案。
  • 抱歉不熟悉 Stack Overflow
猜你喜欢
  • 2022-07-06
  • 2021-10-06
  • 2014-12-26
  • 2020-01-24
  • 2019-07-12
  • 2021-07-21
  • 2020-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多