尝试在WPF中打开新窗口时出错

问题描述 投票:0回答:1

我正在编写一个收银系统,我想打开一个付款窗口,在此您完成付款,但是当我尝试打开该窗口时,当我单击按钮以打开新窗口时,出现System.ArgumentException: 'Must disconnect specified child from current parent Visual before attaching to new parent Visual.'错误。这是我的代码:

  private void PaymentButton_Click(object sender, RoutedEventArgs e)
        {

            if (!PaymentWindowOpen)
            {
                PaymentWindow Window = new PaymentWindow(this);
                Window.Show();//this is where the error shows
                PaymentWindowOpen = true;
            }
        }

这是按钮的代码。这是付款窗口类的代码:

   public partial class PaymentWindow : Window
    {

        public MainWindow mainWindow = new MainWindow();
        public PaymentWindow(MainWindow mainWindow)
        {
            InitializeComponent();
            this.mainWindow = mainWindow;
            PaymentPrice.Content = mainWindow.ActualTotal;

        }

        private void FiverButton_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

忽略空方法,在此先感谢您的帮助

c# wpf
1个回答
0
投票
如错误消息所述,ActualTotal只能出现一次,即,您不能在两个不同的窗口中同时显示控件的相同实例。

如果ActualTotal是TextBlock,则可以从其Text属性复制字符串值,而不是将ContentPaymentPrice设置为TextBlock本身:

PaymentPrice.Content = mainWindow.ActualTotal.Text;

© www.soinside.com 2019 - 2024. All rights reserved.