Easier workings with Haley MVVM
This commit is contained in:
@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -6,6 +6,9 @@ using System.Threading.Tasks;
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using Haley.Models;
|
||||||
|
using Haley.Abstractions;
|
||||||
|
using Haley.MVVM;
|
||||||
|
|
||||||
namespace QuickMVVMSetup;
|
namespace QuickMVVMSetup;
|
||||||
|
|
||||||
@ -28,7 +31,7 @@ public class MainVM : ChangeNotifier
|
|||||||
set { _persons = value; }
|
set { _persons = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddPerson(object person)
|
public void AddPerson()
|
||||||
{
|
{
|
||||||
Persons.Add(TargetPerson); // Adding to collection
|
Persons.Add(TargetPerson); // Adding to collection
|
||||||
TargetPerson = new Person(); //Resetting it
|
TargetPerson = new Person(); //Resetting it
|
||||||
@ -36,12 +39,20 @@ public class MainVM : ChangeNotifier
|
|||||||
|
|
||||||
|
|
||||||
//public ICommand CMDAdd { get; set; }
|
//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()
|
public MainVM()
|
||||||
{
|
{
|
||||||
//CMDAdd = new AddCommand(this);
|
//CMDAdd = new AddCommand(this);
|
||||||
//TargetPerson = new Person();
|
|
||||||
Persons = new ObservableCollection<Person>();
|
Persons = new ObservableCollection<Person>();
|
||||||
|
TargetPerson = new Person();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,9 +43,14 @@
|
|||||||
<TextBlock Text="Age" />
|
<TextBlock Text="Age" />
|
||||||
<TextBox Grid.Column="1" Text="{Binding TargetPerson.Age}"/>
|
<TextBox Grid.Column="1" Text="{Binding TargetPerson.Age}"/>
|
||||||
</ItemsControl>
|
</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>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<ListView Grid.Row="1" Background="#FFDBEFCA" ItemsSource="{Binding Persons}">
|
<ListView x:Name="lstView" Grid.Row="1" Background="#FFDBEFCA" ItemsSource="{Binding Persons}"
|
||||||
|
SelectionMode="Single">
|
||||||
<ListView.ItemTemplate>
|
<ListView.ItemTemplate>
|
||||||
<DataTemplate>
|
<DataTemplate>
|
||||||
<StackPanel Orientation="Horizontal">
|
<StackPanel Orientation="Horizontal">
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
using System;
|
using Haley.Models;
|
||||||
|
using Haley.Abstractions;
|
||||||
|
using Haley.MVVM;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|||||||
@ -11,4 +11,8 @@
|
|||||||
<Compile Remove="AddCommand.cs" />
|
<Compile Remove="AddCommand.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Haley.MVVM" Version="6.2.20" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user