Created
December 16, 2023 00:55
-
-
Save tor4kichi/d46358618e577090eac84f3686ee3223 to your computer and use it in GitHub Desktop.
ListView/GridViewで表示範囲のItemVMのみを列挙する
This file contains hidden or 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
| #nullable enable | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Windows.Input; | |
| using Windows.UI.Xaml; | |
| using Windows.UI.Xaml.Controls; | |
| namespace Starryboard.Views.Behaviors; | |
| public static partial class ListViewExtensions | |
| { | |
| public static ICollection<object>? GetRealizedCollection(DependencyObject obj) | |
| { | |
| return (ICollection<object>?)obj.GetValue(RealizedCollectionProperty); | |
| } | |
| public static void SetRealizedCollection(DependencyObject obj, ICollection<object> value) | |
| { | |
| obj.SetValue(RealizedCollectionProperty, value); | |
| } | |
| public static readonly DependencyProperty RealizedCollectionProperty = | |
| DependencyProperty.RegisterAttached("RealizedCollection", typeof(ICollection<object>), typeof(ListViewExtensions), new PropertyMetadata(null, OnRealizedCollectionChanged)); | |
| private static void OnRealizedCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
| { | |
| var list = (ListViewBase)d; | |
| if (e.OldValue is ICollection<object> oldCollection) | |
| { | |
| list.ContainerContentChanging -= List_ContainerContentChanging; | |
| } | |
| if (e.NewValue is ICollection<object> collection) | |
| { | |
| list.ContainerContentChanging += List_ContainerContentChanging; | |
| } | |
| static void List_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args) | |
| { | |
| var collection = GetRealizedCollection(sender); | |
| if (collection == null) | |
| { | |
| sender.ContainerContentChanging -= List_ContainerContentChanging; | |
| return; | |
| } | |
| if (args.InRecycleQueue) | |
| { | |
| collection.Remove(args.Item); | |
| } | |
| else | |
| { | |
| collection.Add(args.Item); | |
| } | |
| } | |
| } | |
| public static ICommand? GetRealizedCommand(DependencyObject obj) | |
| { | |
| return (ICommand?)obj.GetValue(RealizedCommandProperty); | |
| } | |
| public static void SetRealizedCommand(DependencyObject obj, ICommand? value) | |
| { | |
| obj.SetValue(RealizedCommandProperty, value); | |
| } | |
| public static readonly DependencyProperty RealizedCommandProperty = | |
| DependencyProperty.RegisterAttached("RealizedCommand", typeof(ICommand), typeof(ListViewExtensions), new PropertyMetadata(null, OnRealizeCommandPropertyChanged)); | |
| public static ICommand? GetDerealizedCommand(DependencyObject obj) | |
| { | |
| return (ICommand?)obj.GetValue(DerealizedCommandProperty); | |
| } | |
| public static void SetDerealizedCommand(DependencyObject obj, ICommand? value) | |
| { | |
| obj.SetValue(DerealizedCommandProperty, value); | |
| } | |
| public static readonly DependencyProperty DerealizedCommandProperty = | |
| DependencyProperty.RegisterAttached("DerealizedCommand", typeof(ICommand), typeof(ListViewExtensions), new PropertyMetadata(null, OnRealizeCommandPropertyChanged)); | |
| private static void OnRealizeCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
| { | |
| var list = (ListViewBase)d; | |
| if (e.OldValue is ICommand oldCommand) | |
| { | |
| list.ContainerContentChanging -= ForCommand_ContainerContentChanging; | |
| } | |
| if (e.NewValue is ICommand newCommand) | |
| { | |
| list.ContainerContentChanging -= ForCommand_ContainerContentChanging; | |
| list.ContainerContentChanging += ForCommand_ContainerContentChanging; | |
| } | |
| } | |
| private static void ForCommand_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args) | |
| { | |
| if ((args.InRecycleQueue ? GetDerealizedCommand(sender) : GetRealizedCommand(sender)) is { } command | |
| && command.CanExecute(args.Item) | |
| ) | |
| { | |
| command.Execute(args.Item); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment