【问题标题】:WPF passing data from one window to anotherWPF 将数据从一个窗口传递到另一个窗口
【发布时间】:2018-11-19 15:12:45
【问题描述】:

我有一个程序,我从 CSV 加载数据并将它们保存到列表。然后我将它们加载到列表框和 texboxes。我创建了“新建”按钮以将新项目添加到列表并在列表框和 texboxes 中显示新项目。但是每次我创建新项目时,我都会删除我以前的列表。谁能帮助我如何添加新项目并保留我的所有数据?

第二个窗口:

    public partial class New : Window
    {
    AddDataFromImport data = new AddDataFromImport();
    private string centName;
    private string centCode;
    private string centDesc;

    public New()
    {
        InitializeComponent();
    }

    private void BtnSave_Click(object sender, RoutedEventArgs e)
    {
        Validate();
        MainWindow mainWindow = new MainWindow(centName,centCode,centDesc);
        mainWindow.Show();
        this.Close();
    }
    }

第二个窗口 XML:

<Button x:Name="BtnSave" Content="Save" Grid.Row="5" Grid.Column="1" Width="150" Height="30" Click="BtnSave_Click"></Button>

主窗口:

    public partial class MainWindow : Window
{
    public AddDataFromImport meetingData = new AddDataFromImport();
    private Centre selectedCentre = null;
    private Room selectedRoom = null;

    public MainWindow()
    {
        InitializeComponent();
    }

    public MainWindow(string nameNewCent, string codeNewCent, string descNewCent) : this()
    {
        Centre cent = new Centre(nameNewCent, codeNewCent, descNewCent);
        meetingData.MeetingCentres.Add(cent);
        ListOfCentres.ItemsSource = meetingData.MeetingCentres;
    }

    private void SelectCheck(object sender, EventArgs e)
    {
        MyCheckBox.IsChecked = true;
    }

    // Load import
    private void ImportData_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();
        Nullable<bool> result = openFileDlg.ShowDialog();
        if (result == true)
        {
            var filename = openFileDlg.FileName;
            meetingData.CreateDataStrucuturtes(filename);
            ListOfCentres.ItemsSource = meetingData.MeetingCentres;
        }
    }

    // Fill center columns
    public void ListOfCentres_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        selectedCentre = (Centre)ListOfCentres.SelectedItem;
        if (selectedCentre != null)
        {
            TBoxName.Text = selectedCentre.Name;
            TBoxCode.Text = selectedCentre.Code;
            TBoxDescription.Text = selectedCentre.Description;
        }
    }

    // Fill rooms from selected centre
    private void TBoxName_TextChanged(object sender, TextChangedEventArgs e)
    {
        if ((Centre)ListOfCentres.SelectedItem != null)
            ListOfRooms.ItemsSource = ((Centre)ListOfCentres.SelectedItem).RoomsInCentre;
    }

    // Create new centre
    private void BtnNew_Click(object sender, RoutedEventArgs e)
    {
        New newWindow = new New();
        newWindow.Show();
    }
    }

主窗口 XML:

    <StackPanel HorizontalAlignment="Left" Height="120" Margin="10,51,0,0" VerticalAlignment="Top" Width="340">
        <ListBox x:Name="ListOfCentres" DisplayMemberPath="CenterName" Height="119" HorizontalAlignment="Left" Width="340" ItemsSource="{Binding Path=Centres}" SelectionChanged="ListOfCentres_SelectionChanged"/>
    </StackPanel>

            <Button x:Name="BtnNew" Content="New" Width="70" Margin="0,5,0,0" Height="21" Click="BtnNew_Click"></Button>

【问题讨论】:

  • 为什么要在对话框中创建新的主窗口?不清楚是什么意思“每次我创建新项目时,我都会删除我以前的列表”。什么清单?你怎么看它被删除了?
  • 问题太多了,可能太宽泛了。我的建议是:阅读 MVVM 并修复您的设计(也许您需要重新开始)。
  • 据我所知,我完全期望观察到的行为。您创建 MainWindow Instance1,填写其列表。然后创建“新”实例,它会创建一个新的 MainWindow Instance2(它具有 empty meetingData)并添加新条目。所以 MainWindow Instance1 的所有条目对于 Instance2 都是未知的。
  • 你为什么要在New 表单的BtnSave_Click 中创建一个主窗口的新实例,使用这一行MainWindow mainWindow = new MainWindow(centName,centCode,centDesc); - 这会创建一个主窗口的新实例,其中包含一个新列表- 既然你没有隐藏/关闭它,你为什么还要尝试显示主窗口
  • 我明白了。我无法将新数据添加到列表中,因此我尝试创建新实例。当我只写 BtnSave_Click 时:Center center = new Centre(name, code, desc); MeetingCentres.Add(中心);列表没有填满。

标签: c# wpf listbox


【解决方案1】:

编辑:强烈建议你去学习更多(不一定是 C#)编程的基础知识并阅读 MVVM 设计模式,但如果你真的想这样做,我不会阻止你的

一个解决方案(虽然它可能有点矫枉过正)是使用Callback 方法。基本上,我们为New 窗口的构造函数提供一个方法,并在我们想要保存新创建的Centre 时执行该函数@

您的新窗口如下所示:

private Action<Centre> _callback;

public New(Action<Centre> callback)
{
    InitializeComponent();
    _callback = callback;
}

private void BtnSave_Click(object sender, RoutedEventArgs e)
{
    if (Validate());
    {
        callback(new Centre(centName, centCode, centDesc);
        this.Close();
    }
    else
        MessageBox.Show("Something didn't validate");
}

您的 MainWindow 代码将是这样的:

private void AddNewCentre(Centre centre)
{
    meetingData.MeetingCentres.Add(centre);
}

private void BtnNew_Click(object sender, RoutedEventArgs e)
{
    New newWindow = new New(AddNewCentre);
    newWindow.Show();
}

解释:使用delegates(就像我们正在使用的Action),您可以将方法作为参数传递。在这里,我们传递了将新中心添加到新视图的方法。一旦我们添加了一个新的中心,我们Validate();(我假设这会验证输入的数据,如果数据正确则返回一个布尔值 true/false),如果我们成功地做到了,我们调用我们给出的方法作为将新中心添加到列表中然后关闭窗口的参数

【讨论】:

  • 其实OP应该使用MVVM,从此过上幸福的生活。
  • @Fildor 我理解他应该,并且我理解 MVVM 应该用于 WPF,但是有些人没有足够的 C# 或编程知识(还)来理解模式。似乎 OP 想在不必使用 WinForms 的情况下使用 GUI
  • 学习如何正确地做事比学习如何正确地做事有什么好处?如果OP缺乏知识,我们应该鼓励和帮助他获得知识。如果这超出了他的头脑,他应该从更简单的事情开始。
  • 问题是这是学校作业,所以我必须坚持到明天。确实,我在几乎不了解 OOP 或 MVVM 的情况下这样做。
  • @Fildor 我同意它的质量会非常低,但这似乎是 OP 所学课程的标准。成为新人并不羞耻。 (没有冒犯)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多