Files

116 lines
2.9 KiB
C#

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using Avalonia.Controls;
using Avalonia.Interactivity;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace SimpleNote;
public class ViewModel : ObservableObject
{
private double _windowWidth = 200;
public static double _fontSize = 20;
private double _noteWidth;
public object? LastFocusedNote;
public ObservableCollection<Note> Notes { get; set; } = new ObservableCollection<Note>();
public ICommand AddButtonPressedCommand { get; }
public ViewModel()
{
AddButtonPressedCommand = new RelayCommand(AddButtonPressedCommandEvent);
}
public double WindowWidth
{
get => _windowWidth;
set => SetProperty(ref _windowWidth, value);
}
public double NoteWidth
{
get => _noteWidth;
set => SetProperty(ref _noteWidth, value);
}
public double FontSize
{
get => _fontSize;
set => SetProperty(ref _fontSize, value);
}
private void AddButtonPressedCommandEvent(object? sender)
{
AddNote();
}
//[RelayCommand]
private void AddNote()
{
// Füge eine neue Note zur Collection hinzu
Note note = new Note
{
Width = MainWindow.TextWidth("New Note"), Content = "New Note", FontSize = FontSize,
};
note.RemoveButtonClicked = new RelayCommand(o => RemoveNote(note));
Notes.Add(note);
}
private void RemoveNote(Note note)
{
if (Notes.Contains(note))
{
Notes.Remove(note);
}
}
public event PropertyChangedEventHandler? PropertyChanged;
}
public class Note : ObservableObject
{
public double Width { get; set; }
public string _content;
public double FontSize { get; set; }
public RelayCommand RemoveButtonClicked { get; set; }
public string Content
{
get => _content;
set
{
_content = value;
Width = MainWindow.TextWidth(_content);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Content)));
}
}
public event PropertyChangedEventHandler? PropertyChanged;
}
public class RelayCommand : ICommand
{
private readonly Action<object?> _execute;
private readonly Func<object?, bool>? _canExecute;
public event EventHandler? CanExecuteChanged;
public RelayCommand(Action<object?> execute, Func<object?, bool>? canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object? parameter) =>
_canExecute?.Invoke(parameter) ?? true;
public void Execute(object? parameter) =>
_execute(parameter);
public void RaiseCanExecuteChanged() =>
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}