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 Notes { get; set; } = new ObservableCollection(); 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 _execute; private readonly Func? _canExecute; public event EventHandler? CanExecuteChanged; public RelayCommand(Action execute, Func? 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); }