Easier workings with Haley MVVM

This commit is contained in:
2022-08-08 09:05:30 +02:00
parent 3b346b5782
commit f8bc61327c
6 changed files with 29 additions and 61 deletions

View File

@ -1,20 +0,0 @@
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));
}
}
}

View File

@ -6,6 +6,9 @@ using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Input;
using System.Collections.ObjectModel;
using Haley.Models;
using Haley.Abstractions;
using Haley.MVVM;
namespace QuickMVVMSetup;
@ -28,7 +31,7 @@ public class MainVM : ChangeNotifier
set { _persons = value; }
}
public void AddPerson(object person)
public void AddPerson()
{
Persons.Add(TargetPerson); // Adding to collection
TargetPerson = new Person(); //Resetting it
@ -36,12 +39,20 @@ public class MainVM : ChangeNotifier
//public ICommand CMDAdd { get; set; }
public ICommand CMDAdd => new RelayCommand(AddPerson,null);
public ICommand CMDAdd => new DelegateCommand(AddPerson);
public ICommand CMDDelete => new DelegateCommand<Person>(DeletePerson);
private void DeletePerson(Person obj)
{
if (obj == null) return;
if (!Persons.Contains(obj)) return;
Persons.Remove(obj);
}
public MainVM()
{
//CMDAdd = new AddCommand(this);
//TargetPerson = new Person();
Persons = new ObservableCollection<Person>();
TargetPerson = new Person();
}
}

View File

@ -43,9 +43,14 @@
<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 Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Right" Margin="5,20">
<Button Content="Add" Height="30" Width="120" Margin="5" HorizontalAlignment="Right" Command="{Binding CMDAdd}"/>
<Button Content="Delete" Height="30" Width="120" Margin="5" HorizontalAlignment="Right"
CommandParameter="{Binding ElementName=lstView,Path=SelectedItem}" Command="{Binding CMDDelete}"/>
</StackPanel>
<ListView Grid.Row="1" Background="#FFDBEFCA" ItemsSource="{Binding Persons}">
</StackPanel>
<ListView x:Name="lstView" Grid.Row="1" Background="#FFDBEFCA" ItemsSource="{Binding Persons}"
SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">

View File

@ -1,4 +1,7 @@
using System;
using Haley.Models;
using Haley.Abstractions;
using Haley.MVVM;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

View File

@ -11,4 +11,8 @@
<Compile Remove="AddCommand.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Haley.MVVM" Version="6.2.20" />
</ItemGroup>
</Project>

View File

@ -1,35 +0,0 @@
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;
}
}
}