我正在开发一个使用 Wcf 从数据库中获取数据的界面,我遇到了以下问题,我正在使用动态窗口来显示信息以及一些日期时间按钮和关闭等...
但是,当我关闭窗口并重新打开它时,它会将所有内容添加回已经存在的表单中,这会产生双重影响并导致一些问题,任何建议都会很棒,在此先感谢
这里有一些代码可以更好地描述我所拥有的......
这是我的主要表单上的按钮,它创建了新窗口
private void butMoreInf_Click(object sender, RoutedEventArgs e)
{
winMain.DataContext = null;
winMainContainer.DataContext = null;
winMainContainerLeft.DataContext = null;
winMainContainerRight.DataContext = null;
datGrid.DataContext = null;
//Generate the new window and panel to go within the window
winMain.Height = 750;
winMain.Width = 950;
winMainContainer.Width = 1000;
winMainContainer.HorizontalAlignment = HorizontalAlignment.Left;
winMainContainerLeft.Width = 120;
winMainContainerLeft.HorizontalAlignment = HorizontalAlignment.Left;
winMainContainerRight.Width = 880;
winMainContainerRight.HorizontalAlignment = HorizontalAlignment.Left;
//Generate the datagrid to contain the date to be changed etc.
datGrid.Margin = new Thickness(0, 12, 12, 12);
datGrid.HorizontalAlignment = HorizontalAlignment.Right;
//Generate a datepicker (start) and label
Label labS = new Label();
labS.Content = "Pick a start date";
labS.Width = 100;
labS.Margin = new Thickness(12, 1, 0, 1);
labS.HorizontalAlignment = HorizontalAlignment.Left;
DatePicker datPickStart = new DatePicker();
datPickStart.Width = 100;
datPickStart.Margin = new Thickness(12, 12, 1, 0);
datPickStart.HorizontalAlignment = HorizontalAlignment.Left;
datPickStart.SelectedDate = DateTime.Now.AddDays(-7);
datStartPick = datPickStart.SelectedDate == null ? DateTime.Now : Convert.ToDateTime(datPickStart.SelectedDate);
//Generate a datepicker (end) and label
Label labE = new Label();
labE.Content = "Pick an end date";
labE.Width = 100;
labE.Margin = new Thickness(12, 1, 0, 1);
labE.HorizontalAlignment = HorizontalAlignment.Left;
DatePicker datPickEnd = new DatePicker();
datPickEnd.Width = 100;
datPickEnd.Margin = new Thickness(12, 12, 1, 0);
datPickEnd.HorizontalAlignment = HorizontalAlignment.Left;
datPickEnd.SelectedDate = DateTime.Now;
datEndPick = datPickEnd.SelectedDate == null ? DateTime.Now : Convert.ToDateTime(datPickEnd.SelectedDate);
//Generate dropdown and label for that box
Label labY = new Label();
labY.Content = "";
labY.Width = 100;
labY.Margin = new Thickness(12, 1, 0, 1);
labY.HorizontalAlignment = HorizontalAlignment.Left;
ComboBox txtY = new ComboBox();
txtY.Width = 100;
txtY.Margin = new Thickness(12, 12, 1, 0);
txtY.HorizontalAlignment = HorizontalAlignment.Left;
txtY.SelectedIndex = 0;
txtY.SelectionChanged += CLLoadErrors;
txtY.SelectedIndex = 0;
//Generate error list button
Button butError = new Button();
butError.Width = 100;
butError.Margin = new Thickness(12, 12, 1, 0);
butError.HorizontalAlignment = HorizontalAlignment.Left;
butError.Content = "Get Errors";
butError.Click += CLLoadErrors;
//Generate clear button
Button butClear = new Button();
butClear.Width = 100;
butClear.Margin = new Thickness(12, 12, 1, 0);
butClear.HorizontalAlignment = HorizontalAlignment.Left;
butClear.Content = "Clear Grid";
butClear.Click += clearDataGrid;
//Generate close button
Button butClose = new Button();
butClose.Width = 100;
butClose.Margin = new Thickness(12, 12, 1, 0);
butClose.HorizontalAlignment = HorizontalAlignment.Left;
butClose.Content = "Close";
butClose.Click += CLHide;
//Add elements to the stackpanel / Also updates them before each instance
winMainContainerLeft.UpdateLayout();
winMainContainerLeft.Children.Add(labS);
winMainContainerLeft.Children.Add(datPickStart);
winMainContainerLeft.Children.Add(labE);
winMainContainerLeft.Children.Add(datPickEnd);
winMainContainerLeft.Children.Add(labY);
winMainContainerLeft.Children.Add(txtY);
winMainContainerLeft.Children.Add(butError);
winMainContainerLeft.Children.Add(butClear);
winMainContainerLeft.Children.Add(butClose);
winMainContainerRight.UpdateLayout();
winMainContainerRight.Children.Remove(datGrid);
winMainContainerRight.Children.Add(datGrid);
winMainContainer.UpdateLayout();
winMainContainer.Orientation = Orientation.Horizontal;
winMainContainer.Children.Remove(winMainContainerLeft);
winMainContainer.Children.Add(winMainContainerLeft);
winMainContainer.Children.Remove(winMainContainerRight);
winMainContainer.Children.Add(winMainContainerRight);
winMain.ResizeMode = ResizeMode.NoResize;
//Display the new form
winMain.UpdateLayout();
winMain.Content = winMainContainer;
winMain.Show();
datGrid.MouseDoubleClick += datGridDClick;
txtY.SelectionChanged += new SelectionChangedEventHandler(txtY_SelectionChanged);
}
如果需要,将提供更多代码。