Files
cmerkens d856e8f976 Fixes
2025-01-09 14:49:40 +01:00

117 lines
2.5 KiB
C#

using System.Reflection;
using System.Windows;
using System.Windows.Input;
using Client.Views;
namespace Client.ViewModels;
public class AddAndEditViewModel : ViewModelBase
{
// Konstruktor
public AddAndEditViewModel(WindowAddAndEdit windowAddAndEdit, App application)
{
this._view = windowAddAndEdit;
this._application = application;
this._game = new Game();
this.OkCommand = new RelayCommand(ExecuteOkCommand);
this.CancelCommand = new RelayCommand(ExecuteCancelCommand);
}
// Methode und Attribute zum Starten des Fensters
private WindowAddAndEdit _view;
private App _application;
private void Initialize()
{
this._view.DataContext = this;
this._view.ShowDialog();
}
public Game AddGame()
{
this.Initialize();
this._game.Id = 0; // Neuem Spiel wird von der Datenbank eine Id vergeben
return this._game;
}
public Game EditGame(Game game)
{
this._game = game;
this.Initialize();
return this._game;
}
// Attribute für die ausfüllbaren Felder
private Game _game;
public string Titel
{
get { return _game.Titel; }
set
{
this._game.Titel = value;
OnPropertyChanged();
}
}
public string Kommentar
{
get { return _game.Kommentar; }
set
{
this._game.Kommentar = value;
OnPropertyChanged();
}
}
public Zustand[] Zustaende
{
get { return (Zustand[])Enum.GetValues(typeof(Zustand)); }
}
public Zustand Zustand
{
get { return _game.Zustand; }
set
{
this._game.Zustand = value;
OnPropertyChanged();
}
}
// Methoden und Attribute für die Buttons
public bool dialogResult { get; set; }
public ICommand OkCommand { get; }
private void ExecuteOkCommand(object command)
{
if (string.IsNullOrWhiteSpace(this._game.Titel))
{
MessageBox.Show(
"Bitte geben Sie einen gültigen Titel ein.",
"Ungültige Eingabe",
MessageBoxButton.OK,
MessageBoxImage.Warning
);
}
else
{
this.dialogResult = true;
this._view.Close();
}
}
public ICommand CancelCommand { get; }
private void ExecuteCancelCommand(object command)
{
this.dialogResult = false;
this._view.Close();
}
}