12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System.Collections;
- using System.Collections.ObjectModel;
- using CommunityToolkit.Mvvm.ComponentModel;
- using CommunityToolkit.Mvvm.Input;
- using Waaagh.Behaviors;
- namespace WpfSample.ViewModels {
- internal partial class MainViewModel: ObservableObject {
- public MainViewModel() {
- Random random = new Random();
- for (int i = random.Next(1, 10); i >= 0; --i) {
- Groups.Add(new DemoGroupViewModel() {
- Name = $"G{i}",
- });
- }
- for (int i = random.Next(1, 10); i >= 0; --i) {
- Groups2.Add(new DemoGroupViewModel() {
- Name = $"G{i}",
- });
- }
- }
- [ObservableProperty]
- private string message = string.Empty;
- public ObservableCollection<DemoGroupViewModel> Groups { get; } = new ObservableCollection<DemoGroupViewModel>();
- public ObservableCollection<DemoGroupViewModel> Groups2 { get; } = new ObservableCollection<DemoGroupViewModel>();
- class Test {
- public string Name { get; set; } = string.Empty;
- }
- [RelayCommand]
- private void AddGroup() {
- Groups.Add(new DemoGroupViewModel() {
- Name = $"GX",
- });
- Groups2.Add(new DemoGroupViewModel() {
- Name = $"GY",
- });
- }
- [RelayCommand]
- private void AddItem() {
- Groups[0].AddItemCommand?.Execute(null);
- }
- public ItemsDraggable.DoEffectsCallbackHandler DoMoveEffectFunc { get; } = (object sender, ItemsDraggable.DoEffectsCallbackArgs args) => {
- object sourceData = args.SourceData;
- object? targetData = args.TargetData;
- IList sourceCollection = args.SourceCollection;
- IList targetCollection = args.TargetCollection;
- int oldIndex = sourceCollection.IndexOf(sourceData);
- int newIndex = targetCollection.IndexOf(targetData);
- if (newIndex == -1) {
- newIndex = targetCollection.Count - 1;
- }
- if (object.ReferenceEquals(sourceCollection, targetCollection)) {
- if (oldIndex == newIndex) {
- return;
- }
- // 同个集合, 移除元素后下标可能变化
- if (oldIndex < newIndex) {
- newIndex -= 1;
- }
- }
- sourceCollection.RemoveAt(oldIndex);
- targetCollection.Insert(newIndex + 1, sourceData);
- };
- }
- }
|