XAML:指定的元素已经是另一个元素的逻辑子元素。首先断开它

时间:2014-03-06 06:23:01

标签: c# wpf visual-studio xaml

我正在wpf中建立一个自定义模态框 ModalBOX Example

我希望用户使用一些自定义模态元素从XAML添加Modal Children Control element。 因为我们无法在主窗口内创建子窗口。 所以我间接使用xaml和c#。

以下是我的示例代码实施模式

MainWindow.xaml

<local:ModalWindow>
        <Border Background="Red" Width="400" Height="400">
            <Button Width="110"></Button>
        </Border>
</local:ModalWindow>

MainWindow.xaml.cs

class ModalWindow : StackPanel
{

    public ModalWindow() 
    {

        StackPanel myGrid = this;
        this.Height = 400;
        this.Width = 400; 
        Window myWindow = new MyWindow(this);

    }

 class MyWindow : Window
{

    public MyWindow(object x) : base()
    {
       this.WindowState = WindowState.Maximized;
        this.AllowsTransparency = true;
        this.WindowStyle = WindowStyle.None;
        this.Opacity = 0.7;
        this.Background = Brushes.Black;
        this.AddChild(x);
        this.ShowDialog();

    }
}

加载时会显示RunTimeError

 Error: "Specified element is already the logical child of another element. Disconnect it first"


有没有办法先从MainWindow断开此节点,然后将此Exact节点作为其子节点添加到我的新窗口。

请帮助!

0 个答案:

没有答案
相关问题