Add project files.
This commit is contained in:
9
App.xaml
Normal file
9
App.xaml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<Application x:Class="MVVM_DEMO.App"
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:local="clr-namespace:MVVM_DEMO"
|
||||||
|
StartupUri="/Views/MainWindow.xaml">
|
||||||
|
<Application.Resources>
|
||||||
|
|
||||||
|
</Application.Resources>
|
||||||
|
</Application>
|
||||||
14
App.xaml.cs
Normal file
14
App.xaml.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System.Configuration;
|
||||||
|
using System.Data;
|
||||||
|
using System.Windows;
|
||||||
|
|
||||||
|
namespace MVVM_DEMO
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Interaction logic for App.xaml
|
||||||
|
/// </summary>
|
||||||
|
public partial class App : Application
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
10
AssemblyInfo.cs
Normal file
10
AssemblyInfo.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using System.Windows;
|
||||||
|
|
||||||
|
[assembly: ThemeInfo(
|
||||||
|
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||||
|
//(used if a resource is not found in the page,
|
||||||
|
// or application resource dictionaries)
|
||||||
|
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||||
|
//(used if a resource is not found in the page,
|
||||||
|
// app, or any theme specific resource dictionaries)
|
||||||
|
)]
|
||||||
15
MVVM_DEMO.csproj
Normal file
15
MVVM_DEMO.csproj
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>WinExe</OutputType>
|
||||||
|
<TargetFramework>net8.0-windows</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<UseWPF>true</UseWPF>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="ValueConverters\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
25
MVVM_DEMO.sln
Normal file
25
MVVM_DEMO.sln
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.13.35919.96 d17.13
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVM_DEMO", "MVVM_DEMO.csproj", "{FBB9EB22-3115-4366-8C35-A300DA922B06}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{FBB9EB22-3115-4366-8C35-A300DA922B06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{FBB9EB22-3115-4366-8C35-A300DA922B06}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{FBB9EB22-3115-4366-8C35-A300DA922B06}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{FBB9EB22-3115-4366-8C35-A300DA922B06}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {A0C2E5CF-A025-4D96-B992-801A8719AA75}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
15
Models/Product.cs
Normal file
15
Models/Product.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MVVM_DEMO.Models
|
||||||
|
{
|
||||||
|
public class Product
|
||||||
|
{
|
||||||
|
public string ProductName { get; set; }
|
||||||
|
public double Price { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
50
ViewModels/MainViewModel.cs
Normal file
50
ViewModels/MainViewModel.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MVVM_DEMO.Models;
|
||||||
|
|
||||||
|
namespace MVVM_DEMO.ViewModels
|
||||||
|
{
|
||||||
|
public class MainViewModel : INotifyPropertyChanged
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
private ObservableCollection<Product> _products;
|
||||||
|
|
||||||
|
public event PropertyChangedEventHandler? PropertyChanged;
|
||||||
|
|
||||||
|
public void OnPropertyChanged(string propertyName)
|
||||||
|
{
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||||
|
}
|
||||||
|
|
||||||
|
// constructor
|
||||||
|
|
||||||
|
public MainViewModel()
|
||||||
|
{
|
||||||
|
_products = new ObservableCollection<Product>();
|
||||||
|
LoadData();
|
||||||
|
}
|
||||||
|
|
||||||
|
// read data
|
||||||
|
private void LoadData()
|
||||||
|
{
|
||||||
|
_products.Add(new Product { ProductName = "Product 1", Price = 10.0 });
|
||||||
|
_products.Add(new Product { ProductName = "Product 2", Price = 20.0 });
|
||||||
|
_products.Add(new Product { ProductName = "Product 3", Price = 30.0 });
|
||||||
|
Products = _products;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// properties
|
||||||
|
public ObservableCollection<Product> Products { get; set; }
|
||||||
|
|
||||||
|
public string productName { get; set; }
|
||||||
|
public int productPrice { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
45
Views/MainWindow.xaml
Normal file
45
Views/MainWindow.xaml
Normal 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
59
Views/MainWindow.xaml.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user