Add project files.

This commit is contained in:
Mark Kors
2025-11-12 15:16:52 +01:00
parent 46b69812a0
commit fe6e2cfcff
9 changed files with 242 additions and 0 deletions

45
Views/MainWindow.xaml Normal file
View File

@@ -0,0 +1,45 @@
<Window x:Class="MVVM_DEMO.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVM_DEMO"
xmlns:vm="clr-namespace:MVVM_DEMO.ViewModels"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<Grid>
<StackPanel>
<ComboBox x:Name="comboBox"
Width="200"
Height="30"
Margin="10"
VerticalAlignment="Top"
ItemsSource="{Binding Products}"
DisplayMemberPath="ProductName" />
<Button x:Name="btnAddProduct"
Content="Add Product"
Width="100px"
Height="25px"
Click="btnAddProduct_Click" />
<Label Content="Selected Product Details"
FontWeight="Bold"
FontSize="16"
Margin="10" />
<TextBox x:Name="txtProductName"
Width="200"
Height="30"
Text="{Binding productName}"
/>
<TextBox x:Name="txtProductPrice"
Width="200"
Height="30"
Text="{Binding productPrice}" />
</StackPanel>
</Grid>
</Window>

59
Views/MainWindow.xaml.cs Normal file
View File

@@ -0,0 +1,59 @@
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
{
MainViewModel viewModel = new MainViewModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = viewModel;
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)
{
viewModel.productName= ((Product)comboBox.SelectedItem).ProductName;
viewModel.productPrice = (int)((Product)comboBox.SelectedItem).Price;
viewModel.OnPropertyChanged("productName");
viewModel.OnPropertyChanged("productPrice");
}
}
private void btnAddProduct_Click(object sender, RoutedEventArgs e)
{
// toevoegen van een product in het viewmodel
Product p = new Product();
p.ProductName = $"Product {viewModel.Products.Count + 1}";
// generate a random price between 10 and 100
Random rand = new Random();
p.Price = rand.Next(10, 100);
viewModel.Products.Add(p);
}
}
}