【发布时间】:2021-01-10 22:19:08
【问题描述】:
我试图在 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