I'm trying to create a little database program in which you can add, modify and remove articles for a news site. There is a MainWindow with a DataGrid to display all the articles. Once the Add button is pressed, a new window opens to let you edit the newly created article. Or the modify button can be pressed to edit the currently selected article. How can i pass the article back to the MainWindow to add it to the ObservableCollection/modify the ObservableCollection?
public partial class MainWindow : Window
{
private int lastIdArtikel = 0;
private Artikel currentSelection = null;
private ObservableCollection<Artikel> ArtikelDatabase = new ObservableCollection<Artikel>();
private void Add_Click(object sender, RoutedEventArgs e)
{
Artikel nieuwArtikel = new Artikel( lastIdArtikels);
ArtikelDatabase.Add(nieuwArtikel);
ArtikelVenster artikelVenster = new ArtikelVenster(ref nieuwArtikel);
artikelVenster.Show();
}
private void Modify_Click(object sender, RoutedEventArgs e)
{
try
{
ArtikelVenster artikelVenster = new ArtikelVenster(ref huidigeSelectie);
artikelVenster.Show();
}
catch (NullReferenceException exception)
{
Console.WriteLine(exception.StackTrace);
}
}
public partial class ArtikelVenster : Window
{
public Artikel artikel { get; private set; }
public int id { get; private set; }
public string title { get; private set; }
public object category { get; private set; }
public string nameJournalist { get; private set; }
public bool mayBePublished { get; private set; }
public bool isPublished { get; private set; }
public string content { get; private set; }
public int length { get; private set; }
public ArtikelVenster(ref Artikel huidigArtikel)
{
InitializeComponent();
artikel = huidigArtikel;
try
{
LaadData();
}
catch (NullReferenceException exception)
{
Console.WriteLine(exception.StackTrace);
}
CategorieBox.SelectedIndex = 2;
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void LaadData()
{
IdLabel.Content = artikel.id;
DatumLabel.Content = artikel.creationDate.ToString();
TitelBox.Text = artikel.title;
CategorieBox.SelectedItem = artikel.category;
NaamJournalistBox.Text = artikel.nameJournalist;
IsGepubliceerdToggle.IsChecked = artikel.isPublished;
MagGepubliceerdWordenToggle.IsChecked = artikel.mayBePublished;
if (artikel.content == null)
{
InhoudBox.Selection.Text = "";
}
else
{
InhoudBox.Selection.Text = artikel.content;
}
}
private void Confirm_Click(object sender, RoutedEventArgs e)
{
if (artikel == null)
{
Artikel nieuwArtikel = new Artikel(id);
nieuwArtikel.updateArtikel(title, category, nameJournalist, mayBePublished, isPublished, content, length);
artikel = nieuwArtikel;
}
else
{
artikel.updateArtikel(title, category, nameJournalist, mayBePublished, isPublished, content, length);
}
this.Close();
}
private void TitelBox_TextChanged(object sender, TextChangedEventArgs e)
{
title = TitelBox.Text;
}
private void CategorieBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
category = CategorieBox.SelectedItem;
}
private void NaamJournalistBox_TextChanged(object sender, TextChangedEventArgs e)
{
nameJournalist = NaamJournalistBox.Text;
}
}
public class Artikel
{
public int id { get; private set; }
public string title { get; private set; }
public object category { get; private set; }
public string nameJournalist { get; private set; }
public bool mayBePublished { get; private set; }
public bool isPublished { get; private set; }
public string content { get; private set; }
public int length { get; private set; }
public DateTime creationDate { get; private set; }
public Artikel(int id)
{
this.id = id;
this.creationDate = DateTime.Now;
}
public void updateArtikel(string title, object category, string nameJournalist, bool mayBePublished, bool isPublished, string content, int length)
{
this.title = title;
this.category = category;
this.nameJournalist = nameJournalist;
this.mayBePublished = mayBePublished;
this.isPublished = isPublished;
this.content = content;
this.length = length;
}
}
}
Some parts are left out to make it more readable. If anymore info is needed, let me know.
CodePudding user response:
Declare an event in your window class
public event EventHandler<Artikel> Confirmed;
Invoke it in the confirm button click handler
//artikel being the object that you want to send back to the main window
Confirmed?.Invoke(this,artikel);
You can add listener from the caller :
artikelVenster.Confirmed =(s, e) =>{//do something with e };
artikelVenster.Show();