我有一个程序,我从CSV加载数据并将其保存到列表。然后我将它们加载到列表框和texbox。我创建了“新建”按钮,以将新项目添加到列表中,并在列表框和texbox中显示新项目。但是,每次创建新项目时,我都会删除以前的列表。谁能帮我如何添加新商品并保留我的所有数据?
第二个窗口:
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>
答案 0 :(得分:0)
编辑:我强烈,建议您继续学习(不一定是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
),您可以将方法作为参数传递。在这里,我们传递了将新中心添加到新视图的方法。一旦我们添加了一个新的Center,我们就Validate();
(我假设这会验证输入的数据并在数据正确的情况下返回布尔值true / false),如果成功完成,我们将调用该方法我们作为参数将新的Center添加到列表中,然后关闭窗口