From 32055107abd4650589563f275602b81b89666d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A4seToatz?= Date: Thu, 7 Nov 2024 08:52:03 +0100 Subject: [PATCH] INotifyPropertyChanged added --- Memory.Gui/MainWindow.xaml | 2 +- Memory.Gui/MainWindow.xaml.cs | 35 +++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/Memory.Gui/MainWindow.xaml b/Memory.Gui/MainWindow.xaml index c0b9df9..689e7bd 100644 --- a/Memory.Gui/MainWindow.xaml +++ b/Memory.Gui/MainWindow.xaml @@ -16,7 +16,7 @@ - + diff --git a/Memory.Gui/MainWindow.xaml.cs b/Memory.Gui/MainWindow.xaml.cs index e9faba0..9b0323b 100644 --- a/Memory.Gui/MainWindow.xaml.cs +++ b/Memory.Gui/MainWindow.xaml.cs @@ -5,10 +5,11 @@ using System.Windows.Controls; using System.Windows.Media; using System.Windows.Data; using System.Windows.Media.Imaging; +using System.ComponentModel; namespace Memory.Gui { - public partial class MainWindow : Window + public partial class MainWindow : Window, INotifyPropertyChanged { public const int SCOREPADDING = 160; public const int SCOREMARGIN = 30; @@ -16,11 +17,21 @@ namespace Memory.Gui public const int GRIDHEIGHT = 500; private Game? game; - private int deckSize = 5; + private int _deckSize = 5; + + public int DeckSize { get => _deckSize; set + { + _deckSize = value; + OnPropertyChanged(nameof(DeckSize)); + } + } + + public event PropertyChangedEventHandler? PropertyChanged; public MainWindow() { InitializeComponent(); + DataContext = this; } private void StartGame(object sender, RoutedEventArgs args) @@ -32,7 +43,7 @@ namespace Memory.Gui } else { - game = new(new ScoreHandler(), name, deckSize); + game = new(new ScoreHandler(), name, DeckSize); ErrorLabel.Visibility = Visibility.Hidden; StartScreen.Visibility = Visibility.Hidden; FinishScreen.Visibility = Visibility.Hidden; @@ -42,17 +53,9 @@ namespace Memory.Gui } } - private void Decrease(object sender, RoutedEventArgs args) - { - deckSize = Math.Max(deckSize - 1, 2); - DeckSize.Content = deckSize; - } - - private void Increase(object sender, RoutedEventArgs args) - { - deckSize = Math.Min(deckSize + 1, 10); - DeckSize.Content = deckSize; - } + protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + private void Decrease(object sender, RoutedEventArgs args) => DeckSize = Math.Max(DeckSize - 1, 2); + private void Increase(object sender, RoutedEventArgs args) => DeckSize = Math.Min(DeckSize + 1, 10); private void DrawScores() { @@ -85,7 +88,7 @@ namespace Memory.Gui { Cards.ColumnDefinitions.Clear(); Cards.RowDefinitions.Clear(); - Deck grid = Game.GetGridSize(deckSize); + Deck grid = Game.GetGridSize(DeckSize); int columns = grid.Columns; int rows = grid.Rows; for (int i = 0; i < columns; i++) @@ -117,7 +120,7 @@ namespace Memory.Gui private void Redraw() { Cards.Children.Clear(); - Deck grid = Game.GetGridSize(deckSize); + Deck grid = Game.GetGridSize(DeckSize); for (int i = 0; i < game!.Cards.Count; i++) { Card card = game.Cards[i];