WPF passing data from one window to another












0














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










share|improve this question






















  • Why do you create new main window in dialog? It's not clear what means " Everytime I create new item I delete my previous List". What list? How do you see it's deleted?
    – Sinatr
    Nov 19 '18 at 15:21










  • So many issues, it may be too broad. My suggestion would be: Read up on MVVM and repair your design (maybe you'll need to start over).
    – Fildor
    Nov 19 '18 at 15:29










  • As far as I can see, I would totally expect the observed behavior. You create MainWindow Instance1, fill its list. Then you create "New" Instance, which creates a new MainWindow Instance2 (which has en empty meetingData) and add the new entry. So all entries of MainWindow Instance1 are unknown to Instance2.
    – Fildor
    Nov 19 '18 at 15:38






  • 1




    Why are you createing a new instance of the main window in the BtnSave_Click of your New form with this line MainWindow mainWindow = new MainWindow(centName,centCode,centDesc); - this creates a new instance of the mainwindow with obvisouly a new list - why do you even try to Show the mainWindow at all since you aren't hiding/closing it
    – Rand Random
    Nov 19 '18 at 15:39












  • I see. I couldn't add new data to the List so I tried to create new instance. When I just write in BtnSave_Click: Centre centre = new Centre(name, code, desc); MeetingCentres.Add(centre); the list isn't filling.
    – Tatarka
    Nov 19 '18 at 16:06


















0














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










share|improve this question






















  • Why do you create new main window in dialog? It's not clear what means " Everytime I create new item I delete my previous List". What list? How do you see it's deleted?
    – Sinatr
    Nov 19 '18 at 15:21










  • So many issues, it may be too broad. My suggestion would be: Read up on MVVM and repair your design (maybe you'll need to start over).
    – Fildor
    Nov 19 '18 at 15:29










  • As far as I can see, I would totally expect the observed behavior. You create MainWindow Instance1, fill its list. Then you create "New" Instance, which creates a new MainWindow Instance2 (which has en empty meetingData) and add the new entry. So all entries of MainWindow Instance1 are unknown to Instance2.
    – Fildor
    Nov 19 '18 at 15:38






  • 1




    Why are you createing a new instance of the main window in the BtnSave_Click of your New form with this line MainWindow mainWindow = new MainWindow(centName,centCode,centDesc); - this creates a new instance of the mainwindow with obvisouly a new list - why do you even try to Show the mainWindow at all since you aren't hiding/closing it
    – Rand Random
    Nov 19 '18 at 15:39












  • I see. I couldn't add new data to the List so I tried to create new instance. When I just write in BtnSave_Click: Centre centre = new Centre(name, code, desc); MeetingCentres.Add(centre); the list isn't filling.
    – Tatarka
    Nov 19 '18 at 16:06
















0












0








0







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










share|improve this question













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







c# wpf listbox






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 15:12









Tatarka

194




194












  • Why do you create new main window in dialog? It's not clear what means " Everytime I create new item I delete my previous List". What list? How do you see it's deleted?
    – Sinatr
    Nov 19 '18 at 15:21










  • So many issues, it may be too broad. My suggestion would be: Read up on MVVM and repair your design (maybe you'll need to start over).
    – Fildor
    Nov 19 '18 at 15:29










  • As far as I can see, I would totally expect the observed behavior. You create MainWindow Instance1, fill its list. Then you create "New" Instance, which creates a new MainWindow Instance2 (which has en empty meetingData) and add the new entry. So all entries of MainWindow Instance1 are unknown to Instance2.
    – Fildor
    Nov 19 '18 at 15:38






  • 1




    Why are you createing a new instance of the main window in the BtnSave_Click of your New form with this line MainWindow mainWindow = new MainWindow(centName,centCode,centDesc); - this creates a new instance of the mainwindow with obvisouly a new list - why do you even try to Show the mainWindow at all since you aren't hiding/closing it
    – Rand Random
    Nov 19 '18 at 15:39












  • I see. I couldn't add new data to the List so I tried to create new instance. When I just write in BtnSave_Click: Centre centre = new Centre(name, code, desc); MeetingCentres.Add(centre); the list isn't filling.
    – Tatarka
    Nov 19 '18 at 16:06




















  • Why do you create new main window in dialog? It's not clear what means " Everytime I create new item I delete my previous List". What list? How do you see it's deleted?
    – Sinatr
    Nov 19 '18 at 15:21










  • So many issues, it may be too broad. My suggestion would be: Read up on MVVM and repair your design (maybe you'll need to start over).
    – Fildor
    Nov 19 '18 at 15:29










  • As far as I can see, I would totally expect the observed behavior. You create MainWindow Instance1, fill its list. Then you create "New" Instance, which creates a new MainWindow Instance2 (which has en empty meetingData) and add the new entry. So all entries of MainWindow Instance1 are unknown to Instance2.
    – Fildor
    Nov 19 '18 at 15:38






  • 1




    Why are you createing a new instance of the main window in the BtnSave_Click of your New form with this line MainWindow mainWindow = new MainWindow(centName,centCode,centDesc); - this creates a new instance of the mainwindow with obvisouly a new list - why do you even try to Show the mainWindow at all since you aren't hiding/closing it
    – Rand Random
    Nov 19 '18 at 15:39












  • I see. I couldn't add new data to the List so I tried to create new instance. When I just write in BtnSave_Click: Centre centre = new Centre(name, code, desc); MeetingCentres.Add(centre); the list isn't filling.
    – Tatarka
    Nov 19 '18 at 16:06


















Why do you create new main window in dialog? It's not clear what means " Everytime I create new item I delete my previous List". What list? How do you see it's deleted?
– Sinatr
Nov 19 '18 at 15:21




Why do you create new main window in dialog? It's not clear what means " Everytime I create new item I delete my previous List". What list? How do you see it's deleted?
– Sinatr
Nov 19 '18 at 15:21












So many issues, it may be too broad. My suggestion would be: Read up on MVVM and repair your design (maybe you'll need to start over).
– Fildor
Nov 19 '18 at 15:29




So many issues, it may be too broad. My suggestion would be: Read up on MVVM and repair your design (maybe you'll need to start over).
– Fildor
Nov 19 '18 at 15:29












As far as I can see, I would totally expect the observed behavior. You create MainWindow Instance1, fill its list. Then you create "New" Instance, which creates a new MainWindow Instance2 (which has en empty meetingData) and add the new entry. So all entries of MainWindow Instance1 are unknown to Instance2.
– Fildor
Nov 19 '18 at 15:38




As far as I can see, I would totally expect the observed behavior. You create MainWindow Instance1, fill its list. Then you create "New" Instance, which creates a new MainWindow Instance2 (which has en empty meetingData) and add the new entry. So all entries of MainWindow Instance1 are unknown to Instance2.
– Fildor
Nov 19 '18 at 15:38




1




1




Why are you createing a new instance of the main window in the BtnSave_Click of your New form with this line MainWindow mainWindow = new MainWindow(centName,centCode,centDesc); - this creates a new instance of the mainwindow with obvisouly a new list - why do you even try to Show the mainWindow at all since you aren't hiding/closing it
– Rand Random
Nov 19 '18 at 15:39






Why are you createing a new instance of the main window in the BtnSave_Click of your New form with this line MainWindow mainWindow = new MainWindow(centName,centCode,centDesc); - this creates a new instance of the mainwindow with obvisouly a new list - why do you even try to Show the mainWindow at all since you aren't hiding/closing it
– Rand Random
Nov 19 '18 at 15:39














I see. I couldn't add new data to the List so I tried to create new instance. When I just write in BtnSave_Click: Centre centre = new Centre(name, code, desc); MeetingCentres.Add(centre); the list isn't filling.
– Tatarka
Nov 19 '18 at 16:06






I see. I couldn't add new data to the List so I tried to create new instance. When I just write in BtnSave_Click: Centre centre = new Centre(name, code, desc); MeetingCentres.Add(centre); the list isn't filling.
– Tatarka
Nov 19 '18 at 16:06














1 Answer
1






active

oldest

votes


















0














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






share|improve this answer























  • Actually, OP should use MVVM and live happily everafter.
    – Fildor
    Nov 19 '18 at 15:42










  • @Fildor I understand he should, and I understand that MVVM should be used for WPF, but some people don't have an advanced enough knowledge of C# or programming (yet) to understand patterns. And it seems like OP wants to play around with a GUI without having to use WinForms
    – MindSwipe
    Nov 19 '18 at 15:46










  • And what would be the benefit in learning how to fiddle a way around doing it correctly over learning how to do it correctly? If OP lacks knowledge, we should encourage and help him to acquire that knowledge. If it's over his head, he should start with something simpler.
    – Fildor
    Nov 19 '18 at 15:50










  • The thing is it's school homework so I have to make it till tomorrow. It's true I am doing this with almost no knowledge of OOP or MVVM.
    – Tatarka
    Nov 19 '18 at 16:02






  • 1




    @Fildor I agree it will be very low quality, but it seems like that's the bar for the course OP is taking. No shame in being new. (No offense taken)
    – MindSwipe
    Nov 19 '18 at 16:11













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377557%2fwpf-passing-data-from-one-window-to-another%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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






share|improve this answer























  • Actually, OP should use MVVM and live happily everafter.
    – Fildor
    Nov 19 '18 at 15:42










  • @Fildor I understand he should, and I understand that MVVM should be used for WPF, but some people don't have an advanced enough knowledge of C# or programming (yet) to understand patterns. And it seems like OP wants to play around with a GUI without having to use WinForms
    – MindSwipe
    Nov 19 '18 at 15:46










  • And what would be the benefit in learning how to fiddle a way around doing it correctly over learning how to do it correctly? If OP lacks knowledge, we should encourage and help him to acquire that knowledge. If it's over his head, he should start with something simpler.
    – Fildor
    Nov 19 '18 at 15:50










  • The thing is it's school homework so I have to make it till tomorrow. It's true I am doing this with almost no knowledge of OOP or MVVM.
    – Tatarka
    Nov 19 '18 at 16:02






  • 1




    @Fildor I agree it will be very low quality, but it seems like that's the bar for the course OP is taking. No shame in being new. (No offense taken)
    – MindSwipe
    Nov 19 '18 at 16:11


















0














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






share|improve this answer























  • Actually, OP should use MVVM and live happily everafter.
    – Fildor
    Nov 19 '18 at 15:42










  • @Fildor I understand he should, and I understand that MVVM should be used for WPF, but some people don't have an advanced enough knowledge of C# or programming (yet) to understand patterns. And it seems like OP wants to play around with a GUI without having to use WinForms
    – MindSwipe
    Nov 19 '18 at 15:46










  • And what would be the benefit in learning how to fiddle a way around doing it correctly over learning how to do it correctly? If OP lacks knowledge, we should encourage and help him to acquire that knowledge. If it's over his head, he should start with something simpler.
    – Fildor
    Nov 19 '18 at 15:50










  • The thing is it's school homework so I have to make it till tomorrow. It's true I am doing this with almost no knowledge of OOP or MVVM.
    – Tatarka
    Nov 19 '18 at 16:02






  • 1




    @Fildor I agree it will be very low quality, but it seems like that's the bar for the course OP is taking. No shame in being new. (No offense taken)
    – MindSwipe
    Nov 19 '18 at 16:11
















0












0








0






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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 16:30

























answered Nov 19 '18 at 15:39









MindSwipe

54313




54313












  • Actually, OP should use MVVM and live happily everafter.
    – Fildor
    Nov 19 '18 at 15:42










  • @Fildor I understand he should, and I understand that MVVM should be used for WPF, but some people don't have an advanced enough knowledge of C# or programming (yet) to understand patterns. And it seems like OP wants to play around with a GUI without having to use WinForms
    – MindSwipe
    Nov 19 '18 at 15:46










  • And what would be the benefit in learning how to fiddle a way around doing it correctly over learning how to do it correctly? If OP lacks knowledge, we should encourage and help him to acquire that knowledge. If it's over his head, he should start with something simpler.
    – Fildor
    Nov 19 '18 at 15:50










  • The thing is it's school homework so I have to make it till tomorrow. It's true I am doing this with almost no knowledge of OOP or MVVM.
    – Tatarka
    Nov 19 '18 at 16:02






  • 1




    @Fildor I agree it will be very low quality, but it seems like that's the bar for the course OP is taking. No shame in being new. (No offense taken)
    – MindSwipe
    Nov 19 '18 at 16:11




















  • Actually, OP should use MVVM and live happily everafter.
    – Fildor
    Nov 19 '18 at 15:42










  • @Fildor I understand he should, and I understand that MVVM should be used for WPF, but some people don't have an advanced enough knowledge of C# or programming (yet) to understand patterns. And it seems like OP wants to play around with a GUI without having to use WinForms
    – MindSwipe
    Nov 19 '18 at 15:46










  • And what would be the benefit in learning how to fiddle a way around doing it correctly over learning how to do it correctly? If OP lacks knowledge, we should encourage and help him to acquire that knowledge. If it's over his head, he should start with something simpler.
    – Fildor
    Nov 19 '18 at 15:50










  • The thing is it's school homework so I have to make it till tomorrow. It's true I am doing this with almost no knowledge of OOP or MVVM.
    – Tatarka
    Nov 19 '18 at 16:02






  • 1




    @Fildor I agree it will be very low quality, but it seems like that's the bar for the course OP is taking. No shame in being new. (No offense taken)
    – MindSwipe
    Nov 19 '18 at 16:11


















Actually, OP should use MVVM and live happily everafter.
– Fildor
Nov 19 '18 at 15:42




Actually, OP should use MVVM and live happily everafter.
– Fildor
Nov 19 '18 at 15:42












@Fildor I understand he should, and I understand that MVVM should be used for WPF, but some people don't have an advanced enough knowledge of C# or programming (yet) to understand patterns. And it seems like OP wants to play around with a GUI without having to use WinForms
– MindSwipe
Nov 19 '18 at 15:46




@Fildor I understand he should, and I understand that MVVM should be used for WPF, but some people don't have an advanced enough knowledge of C# or programming (yet) to understand patterns. And it seems like OP wants to play around with a GUI without having to use WinForms
– MindSwipe
Nov 19 '18 at 15:46












And what would be the benefit in learning how to fiddle a way around doing it correctly over learning how to do it correctly? If OP lacks knowledge, we should encourage and help him to acquire that knowledge. If it's over his head, he should start with something simpler.
– Fildor
Nov 19 '18 at 15:50




And what would be the benefit in learning how to fiddle a way around doing it correctly over learning how to do it correctly? If OP lacks knowledge, we should encourage and help him to acquire that knowledge. If it's over his head, he should start with something simpler.
– Fildor
Nov 19 '18 at 15:50












The thing is it's school homework so I have to make it till tomorrow. It's true I am doing this with almost no knowledge of OOP or MVVM.
– Tatarka
Nov 19 '18 at 16:02




The thing is it's school homework so I have to make it till tomorrow. It's true I am doing this with almost no knowledge of OOP or MVVM.
– Tatarka
Nov 19 '18 at 16:02




1




1




@Fildor I agree it will be very low quality, but it seems like that's the bar for the course OP is taking. No shame in being new. (No offense taken)
– MindSwipe
Nov 19 '18 at 16:11






@Fildor I agree it will be very low quality, but it seems like that's the bar for the course OP is taking. No shame in being new. (No offense taken)
– MindSwipe
Nov 19 '18 at 16:11




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377557%2fwpf-passing-data-from-one-window-to-another%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory