-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A simple base VM has implemented for the sample project to reduce complexity.
- Loading branch information
1 parent
c54d042
commit aaa82e3
Showing
3 changed files
with
81 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace Maui.DataGrid.Sample.ViewModels; | ||
|
||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
using System.Windows.Input; | ||
|
||
public abstract class ViewModelBase : INotifyPropertyChanged | ||
{ | ||
public event PropertyChangedEventHandler PropertyChanged; | ||
private readonly Dictionary<string, object> _properties = new(); | ||
|
||
|
||
public Dictionary<string, ICommand> Commands | ||
{ | ||
get => GetValue<Dictionary<string, ICommand>>(); | ||
private init => SetValue(value); | ||
} | ||
|
||
protected ViewModelBase() | ||
{ | ||
Commands = new(); | ||
} | ||
protected void SetValue(object value, [CallerMemberName] string propertyName = null) | ||
{ | ||
if (_properties.TryGetValue(propertyName!, out var item) && item == value) | ||
{ | ||
return; | ||
} | ||
|
||
_properties[propertyName!] = value; | ||
OnPropertyChanged(propertyName); | ||
} | ||
|
||
protected T GetValue<T>([CallerMemberName] string propertyName = null) | ||
{ | ||
if (_properties.TryGetValue(propertyName!, out var value)) | ||
{ | ||
return (T)value; | ||
} | ||
|
||
return default; | ||
} | ||
|
||
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) => | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | ||
} |