Add project files.

This commit is contained in:
2022-08-08 08:01:47 +02:00
parent 6cdc06a98d
commit 3b346b5782
12 changed files with 346 additions and 0 deletions

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace QuickMVVMSetup
{
public class AddCommand : ICommand
{
public event EventHandler? CanExecuteChanged;
public MainVM assosiatedVM;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
assosiatedVM.AddPerson();
}
public AddCommand(MainVM vm)
{
assosiatedVM = vm;
}
}
}

9
QuickMVVMSetup/App.xaml Normal file
View File

@ -0,0 +1,9 @@
<Application x:Class="QuickMVVMSetup.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:QuickMVVMSetup"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace QuickMVVMSetup
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

View 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)
)]

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Input;
using System.Collections.ObjectModel;
namespace QuickMVVMSetup
{
public class ChangeNotifier : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
}

47
QuickMVVMSetup/MainVM.cs Normal file
View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Input;
using System.Collections.ObjectModel;
namespace QuickMVVMSetup;
public class MainVM : ChangeNotifier
{
private Person person;
public Person TargetPerson
{
get { return person; }
set { person = value; OnPropertyChanged(nameof(TargetPerson)); }
}
private ObservableCollection<Person> _persons; //only when added/or removed (not for internal property changes)
public ObservableCollection<Person> Persons
{
get { return _persons; }
set { _persons = value; }
}
public void AddPerson(object person)
{
Persons.Add(TargetPerson); // Adding to collection
TargetPerson = new Person(); //Resetting it
}
//public ICommand CMDAdd { get; set; }
public ICommand CMDAdd => new RelayCommand(AddPerson,null);
public MainVM()
{
//CMDAdd = new AddCommand(this);
//TargetPerson = new Person();
Persons = new ObservableCollection<Person>();
}
}

View File

@ -0,0 +1,69 @@
<Window x:Class="QuickMVVMSetup.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:QuickMVVMSetup"
mc:Ignorable="d" WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="450" Width="400">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type ItemsControl}">
<Setter Property="Margin" Value="5"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Margin="10">
<ItemsControl>
<TextBlock Text="First Name" />
<TextBox Grid.Column="1" Text="{Binding TargetPerson.FName}"/>
</ItemsControl>
<ItemsControl>
<TextBlock Text="Last Name" />
<TextBox Grid.Column="1" Text="{Binding TargetPerson.LName}"/>
</ItemsControl>
<ItemsControl>
<TextBlock Text="Age" />
<TextBox Grid.Column="1" Text="{Binding TargetPerson.Age}"/>
</ItemsControl>
<Button Content="Add" Height="30" Width="120" Margin="5, 20" HorizontalAlignment="Right" Command="{Binding CMDAdd}"/>
</StackPanel>
<ListView Grid.Row="1" Background="#FFDBEFCA" ItemsSource="{Binding Persons}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FName}" />
<TextBlock Text=" - " Foreground="Blue" />
<TextBlock Text="{Binding LName}" />
<TextBlock Text=" - " Foreground="Blue" />
<TextBlock Text="{Binding Age}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<!--<WrapPanel>
<TextBlock Text="First Name" />
<TextBlock Text=" - " Foreground="Blue" />
<TextBlock Text="Last Name" />
<TextBlock Text=" - " Foreground="Blue" />
<TextBlock Text="Age" />
</WrapPanel>-->
</ListView>
</Grid>
</Window>

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 QuickMVVMSetup
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainVM(); //Connecting view with viewmodel
}
}
}

39
QuickMVVMSetup/Person.cs Normal file
View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QuickMVVMSetup
{
public class Person : ChangeNotifier
{
//poco model & dto model
private string _FName;
public string FName
{
get { return _FName; }
set { _FName = value; OnPropertyChanged(nameof(FName)); }
}
private string _LName;
public string LName
{
get { return _LName; }
set { _LName = value; OnPropertyChanged(nameof(LName)); }
}
private string _Age;
public string Age
{
get { return _Age; }
set { _Age = value; OnPropertyChanged(nameof(Age)); }
}
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<Compile Remove="AddCommand.cs" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace QuickMVVMSetup
{
public class RelayCommand : ICommand
{
public event EventHandler? CanExecuteChanged;
private Action<object> _executeAction;
private Func<object, bool> _canExecuteAction;
public bool CanExecute(object? parameter)
{
if (_canExecuteAction == null)
{
return true;
}
return _canExecuteAction.Invoke(parameter);
}
public void Execute(object? parameter)
{
_executeAction.Invoke(parameter);
}
public RelayCommand(Action<object> executeAction, Func<object, bool> canExecuteFunc)
{
_executeAction = executeAction;
_canExecuteAction = canExecuteFunc;
}
}
}