0

我正在开发一个使用 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);
}

如果需要,将提供更多代码。

4

3 回答 3

1

您一次又一次地重用同一个窗口(为什么?),因此您需要在添加新元素之前清除子集合,否则每次单击按钮都会显示窗口+您添加的每个控件,从而导致双/三/... 控制。

更新:我假设您使用StackPanels(来自您的评论/Children枚举)。Children枚举是一个UIElementCollection,它允许调用该Clear方法。只需在添加新控件之前调用它。(在winMainContainer,winMainContainerRightwinMainContainerLeft)

于 2012-04-10T12:22:57.683 回答
0

你在哪里初始化winMain?它是静态变量还是存在于您当前的形式中?

我猜你一直在向现有对象添加控件(这会导致复制效果)。要解决此问题,您可以重新初始化表单对象

winMain = new WinMainForm();

或者您可以在添加新子项之前删除 winMain 变量上的所有控件

foreach(var c in winMainContainer.children){
  winMainContainer.children.remove(c)
}
于 2012-04-10T12:27:32.013 回答
0

看起来您根本没有创建新窗口,而只是对存在于更高范围内的“winMain”执行操作。因此,每次调用该函数时,您只是在堆栈面板中添加越来越多的子项。

您可以通过在方法开始时执行类似的操作来解决问题。

winMainContainerLeft.Clear();
winMainContainerRight.Clear();
winMainContainer.Children.Clear();

我还注意到您在示例中包含了以下几行:

winMainContainer.Children.Remove(winMainContainerLeft);
winMainContainer.Children.Add(winMainContainerLeft);
winMainContainer.Children.Remove(winMainContainerRight);
winMainContainer.Children.Add(winMainContainerRight);

但是你不需要在添加它们之前删除它们(无论如何这没有任何意义。)

于 2012-04-10T12:27:48.240 回答