Files
mvvm_demo/Views/MainWindow.xaml.cs
Mark Kors ce63f9bff9 Implementeer ICommand patroon met RelayCommand voor MVVM
- Voeg RelayCommand class toe voor herbruikbare ICommand implementatie
- Vervang button click event handler door AddProductCommand in MainViewModel
- Update XAML om Command binding te gebruiken in plaats van Click event
- Verwijder business logic uit code-behind (MainWindow.xaml.cs)
- Los DataContext duplicatie op (was twee keer MainViewModel instantie)

Dit maakt de applicatie beter testbaar en volgt proper MVVM principes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-12 19:11:54 +01:00

47 lines
1.5 KiB
C#

using MVVM_DEMO.Models;
using MVVM_DEMO.ViewModels;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MVVM_DEMO
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// ViewModel is already set in XAML via Window.DataContext
comboBox.SelectionChanged += ComboBox_SelectionChanged;
// initial selection
if (comboBox.Items.Count > 0)
{
comboBox.SelectedIndex = 0;
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// display selected product details
if (comboBox.SelectedItem != null && DataContext is MainViewModel viewModel)
{
viewModel.productName = ((Product)comboBox.SelectedItem).ProductName;
viewModel.productPrice = (int)((Product)comboBox.SelectedItem).Price;
viewModel.OnPropertyChanged("productName");
viewModel.OnPropertyChanged("productPrice");
}
}
// btnAddProduct_Click removed - now using Command binding in XAML
}
}