C# 有没有办法将ContentControl转换为此格式
我正在尝试将XAML实现到CS中。它成功了,但只有一件事我无法解决,那就是: 将其转换为ContentControlC# 有没有办法将ContentControl转换为此格式,c#,this,contentcontrol,C#,This,Contentcontrol,我正在尝试将XAML实现到CS中。它成功了,但只有一件事我无法解决,那就是: 将其转换为ContentControl object parent = this; MessageBoxEx.SetParentWindow(this); 必须设置父窗口,否则我将获得空引用 有人能帮我吗 找到了解决办法 public void InitMessageBox() { // Create the ElementHost control fo
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,我不知道如何在注释中发布代码。也许可以回答您的问题?请编辑您的问题,而不是编写答案。很抱歉,不熟悉堆栈溢出