Tatarka
Tatarka

Reputation: 73

WPF passing data from one window to another

I have a program where I am loading data from CSV and I am saving them to List. Then I am loading them to listbox and to texboxes. I created "New" button to add a new item to the List and show new item in the listbox and texboxes. But Everytime I create new item I delete my previous List. Can anyone please help me how to add a new item and keep all my data?

Second window:

    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();
    }
    }

Second window XML:

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

Main window:

    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();
    }
    }

Main window 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>

WPF

Upvotes: 0

Views: 2040

Answers (1)

MindSwipe
MindSwipe

Reputation: 7940

Edit: I Strongly recommend you go and learn more of the fundamentals of (not necessarily C#) programming and read up on the MVVM design pattern, but if you really want to do it this way, I wont stop you

A solution (although it may be a bit overkill) is to use a Callback method. Basically we give a Method to the Constructor of your New window and the execute that function when we want to save the newly created Centre

Your New window would look like this:

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");
}

And your MainWindow code would be something like this:

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

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

Explanation: With delegates (like the Action we're using) you can pass methods as parameters. And here we're passing the method to add a new Centre to your new view. Once we're done adding a new Centre we Validate(); (I'm assuming this validates the data that was entered and returns a bool true/ false if the data is correct) and if we successfully do that we call the method we gave as a parameter which adds the new Centre to the list and then close the window

Upvotes: 1

Related Questions