diff --git a/Source/HangoverAvalonia/Controls/CheckedListBox.axaml b/Source/HangoverAvalonia/Controls/CheckedListBox.axaml index b7d8c94d..a1750735 100644 --- a/Source/HangoverAvalonia/Controls/CheckedListBox.axaml +++ b/Source/HangoverAvalonia/Controls/CheckedListBox.axaml @@ -4,27 +4,16 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="HangoverAvalonia.Controls.CheckedListBox"> - - - - - - - - - - - - - + + + + + + + diff --git a/Source/HangoverAvalonia/Controls/CheckedListBox.axaml.cs b/Source/HangoverAvalonia/Controls/CheckedListBox.axaml.cs index bdcfcf61..26ea99b8 100644 --- a/Source/HangoverAvalonia/Controls/CheckedListBox.axaml.cs +++ b/Source/HangoverAvalonia/Controls/CheckedListBox.axaml.cs @@ -2,103 +2,18 @@ using Avalonia; using Avalonia.Collections; using Avalonia.Controls; using HangoverAvalonia.ViewModels; -using ReactiveUI; -using System; -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -namespace HangoverAvalonia.Controls +namespace HangoverAvalonia.Controls; + +public partial class CheckedListBox : UserControl { - public partial class CheckedListBox : UserControl + public static readonly StyledProperty> ItemsProperty = + AvaloniaProperty.Register>(nameof(Items)); + + public AvaloniaList Items { get => GetValue(ItemsProperty); set => SetValue(ItemsProperty, value); } + + public CheckedListBox() { - public event EventHandler ItemCheck; - - public static readonly StyledProperty ItemsProperty = - AvaloniaProperty.Register(nameof(Items)); - - public IEnumerable Items { get => GetValue(ItemsProperty); set => SetValue(ItemsProperty, value); } - private CheckedListBoxViewModel _viewModel = new(); - - public IEnumerable CheckedItems => - _viewModel - .CheckboxItems - .Where(i => i.IsChecked) - .Select(i => i.Item); - - public void SetItemChecked(int i, bool isChecked) => _viewModel.CheckboxItems[i].IsChecked = isChecked; - public void SetItemChecked(object item, bool isChecked) - { - var obj = _viewModel.CheckboxItems.SingleOrDefault(i => i.Item == item); - if (obj is not null) - obj.IsChecked = isChecked; - } - - public CheckedListBox() - { - InitializeComponent(); - scroller.DataContext = _viewModel; - _viewModel.CheckedChanged += _viewModel_CheckedChanged; - } - - private void _viewModel_CheckedChanged(object sender, CheckBoxViewModel e) - { - var args = new ItemCheckEventArgs { Item = e.Item, ItemIndex = _viewModel.CheckboxItems.IndexOf(e), IsChecked = e.IsChecked }; - ItemCheck?.Invoke(this, args); - } - - protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) - { - if (change.Property.Name == nameof(Items) && Items != null) - _viewModel.SetItems(Items); - base.OnPropertyChanged(change); - } - - public class CheckedListBoxViewModel : ViewModelBase - { - public event EventHandler CheckedChanged; - public AvaloniaList CheckboxItems { get; private set; } - - public void SetItems(IEnumerable items) - { - UnsubscribeFromItems(CheckboxItems); - CheckboxItems = new(items.OfType().Select(o => new CheckBoxViewModel { Item = o })); - SubscribeToItems(CheckboxItems); - this.RaisePropertyChanged(nameof(CheckboxItems)); - } - - private void SubscribeToItems(IEnumerable objects) - { - foreach (var i in objects.OfType()) - i.PropertyChanged += I_PropertyChanged; - } - - private void UnsubscribeFromItems(AvaloniaList objects) - { - if (objects is null) return; - - foreach (var i in objects) - i.PropertyChanged -= I_PropertyChanged; - } - private void I_PropertyChanged(object sender, PropertyChangedEventArgs e) - { - CheckedChanged?.Invoke(this, (CheckBoxViewModel)sender); - } - } - public class CheckBoxViewModel : ViewModelBase - { - private bool _isChecked; - public bool IsChecked { get => _isChecked; set => this.RaiseAndSetIfChanged(ref _isChecked, value); } - private object _bookText; - public object Item { get => _bookText; set => this.RaiseAndSetIfChanged(ref _bookText, value); } - } - } - - public class ItemCheckEventArgs : EventArgs - { - public int ItemIndex { get; init; } - public bool IsChecked { get; init; } - public object Item { get; init; } + InitializeComponent(); } } diff --git a/Source/HangoverAvalonia/HangoverAvalonia.csproj b/Source/HangoverAvalonia/HangoverAvalonia.csproj index 2f0ea40d..fd506340 100644 --- a/Source/HangoverAvalonia/HangoverAvalonia.csproj +++ b/Source/HangoverAvalonia/HangoverAvalonia.csproj @@ -76,7 +76,6 @@ - diff --git a/Source/HangoverAvalonia/ViewModels/CheckBoxViewModel.cs b/Source/HangoverAvalonia/ViewModels/CheckBoxViewModel.cs new file mode 100644 index 00000000..f21f62b7 --- /dev/null +++ b/Source/HangoverAvalonia/ViewModels/CheckBoxViewModel.cs @@ -0,0 +1,11 @@ +using ReactiveUI; + +namespace HangoverAvalonia.ViewModels; + +public class CheckBoxViewModel : ViewModelBase +{ + private bool _isChecked; + public bool IsChecked { get => _isChecked; set => this.RaiseAndSetIfChanged(ref _isChecked, value); } + private object _bookText; + public object Item { get => _bookText; set => this.RaiseAndSetIfChanged(ref _bookText, value); } +} diff --git a/Source/LibationAvalonia/Controls/CheckedListBox.axaml b/Source/LibationAvalonia/Controls/CheckedListBox.axaml index 66625cbc..a00ad34d 100644 --- a/Source/LibationAvalonia/Controls/CheckedListBox.axaml +++ b/Source/LibationAvalonia/Controls/CheckedListBox.axaml @@ -5,26 +5,15 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="LibationAvalonia.Controls.CheckedListBox"> - - - - - - - - - - - - - + + + + + + + diff --git a/Source/LibationAvalonia/Controls/CheckedListBox.axaml.cs b/Source/LibationAvalonia/Controls/CheckedListBox.axaml.cs index 7f3d07a5..82d88622 100644 --- a/Source/LibationAvalonia/Controls/CheckedListBox.axaml.cs +++ b/Source/LibationAvalonia/Controls/CheckedListBox.axaml.cs @@ -12,24 +12,10 @@ namespace LibationAvalonia.Controls AvaloniaProperty.Register>(nameof(Items)); public AvaloniaList Items { get => GetValue(ItemsProperty); set => SetValue(ItemsProperty, value); } - private CheckedListBoxViewModel _viewModel = new(); public CheckedListBox() { InitializeComponent(); - scroller.DataContext = _viewModel; - } - protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) - { - if (change.Property.Name == nameof(Items) && Items != null) - _viewModel.CheckboxItems = Items; - base.OnPropertyChanged(change); - } - - private class CheckedListBoxViewModel : ViewModelBase - { - private AvaloniaList _checkboxItems; - public AvaloniaList CheckboxItems { get => _checkboxItems; set => this.RaiseAndSetIfChanged(ref _checkboxItems, value); } } } diff --git a/Source/LibationAvalonia/LibationAvalonia.csproj b/Source/LibationAvalonia/LibationAvalonia.csproj index 04741857..0696bdb1 100644 --- a/Source/LibationAvalonia/LibationAvalonia.csproj +++ b/Source/LibationAvalonia/LibationAvalonia.csproj @@ -77,7 +77,6 @@ - diff --git a/Source/LibationAvalonia/Views/ProcessQueueControl.axaml b/Source/LibationAvalonia/Views/ProcessQueueControl.axaml index 9418f2c2..faf576e4 100644 --- a/Source/LibationAvalonia/Views/ProcessQueueControl.axaml +++ b/Source/LibationAvalonia/Views/ProcessQueueControl.axaml @@ -10,23 +10,13 @@ Background="{DynamicResource SystemRegionColor}" x:Class="LibationAvalonia.Views.ProcessQueueControl"> - - - - - - - - - - - - - - + @@ -42,14 +32,13 @@ HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" AllowAutoHide="False"> - + + + + + + +