我正在编写一个收银系统,我想打开一个付款窗口,在此您完成付款,但是当我尝试打开该窗口时,当我单击按钮以打开新窗口时,出现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)
{
}
}
}
忽略空方法,在此先感谢您的帮助
ActualTotal
只能出现一次,即,您不能在两个不同的窗口中同时显示控件的相同实例。如果ActualTotal
是TextBlock,则可以从其Text
属性复制字符串值,而不是将Content
的PaymentPrice
设置为TextBlock
本身:
PaymentPrice.Content = mainWindow.ActualTotal.Text;