C# WPF实现两个ListBox之间Item的拖动

By jerryxjr1220 at 2023-10-23 • 0人收藏 • 244人看过
<hc:GlowWindow x:Class="WPFDragItem.Views.MainWindow"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               xmlns:vm="clr-namespace:WPFDragItem.ViewModels"
               xmlns:hc="https://handyorg.github.io/handycontrol"
               d:DataContext="{d:DesignInstance Type=vm:MainViewModel}"
               mc:Ignorable="d"
               Title="MainWindow" Height="450" Width="800"
               FontFamily="JetBrains Mono"
               FontSize="{StaticResource TextFontSize}">
    <Window.Resources>
    </Window.Resources>
    <hc:SimplePanel>
        <ScrollViewer x:Name="scrollViewer" VerticalScrollBarVisibility="Auto">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <ListBox Grid.Column="0" ItemsSource="{Binding List1}" AllowDrop="True"
                         PreviewMouseLeftButtonDown="ListBox_OnPreviewMouseLeftButtonDown" Drop="Listbox_OnDrop" />
                <ListBox Grid.Column="1" ItemsSource="{Binding List2}" AllowDrop="True" Drop="Listbox_OnDrop"
                         PreviewMouseLeftButtonDown="ListBox_OnPreviewMouseLeftButtonDown" />
            </Grid>
        </ScrollViewer>
    </hc:SimplePanel>
</hc:GlowWindow>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using HandyControl.Controls;
using WPFDragItem.ViewModels;

namespace WPFDragItem.Views;

/// <summary>
///     Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : GlowWindow
{
    private readonly MainViewModel mainViewModel;

    public MainWindow()
    {
        InitializeComponent();

        #region 注册并接受消息

        //WeakReferenceMessenger.Default.Register<CustomizedMessage>(this, (o, m) => { MessageBox.Show("Received!"); });

        #endregion


        mainViewModel = new MainViewModel();
        DataContext = mainViewModel;
    }


    private void Listbox_OnDrop(object sender, DragEventArgs e)
    {
        var data = e.Data.GetData(typeof(string));
        if (mainViewModel.List1.Contains(data as string))
        {
            mainViewModel.List1.Remove(data as string);
            mainViewModel.List2.Add(data as string);
        }

        else if (mainViewModel.List2.Contains(data as string))
        {
            mainViewModel.List1.Add(data as string);
            mainViewModel.List2.Remove(data as string);
        }
    }

    private void ListBox_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        try
        {
            var parent = (ListBox)sender;
            var data = parent.SelectedItems[0];
            DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
        }
        catch (Exception)
        {
            //ignored
        }
    }
}
using System.ComponentModel;
using CommunityToolkit.Mvvm.ComponentModel;

namespace WPFDragItem.ViewModels;

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty] private BindingList<string> _list1;
    [ObservableProperty] private BindingList<string> _list2;

    public MainViewModel()
    {
        List1 = new BindingList<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
        List2 = new BindingList<string> { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
        //发送消息
        //WeakReferenceMessenger.Default.Send(new CustomizedMessage(new MessageModel()));
    }
}

//定义消息
// public class CustomizedMessage : ValueChangedMessage<MessageModel> // PropertyChangedMessage // RequestMessage ( Async + ..)
// {
//     public CustomizedMessage(MessageModel value) : base(value)
//     {
//     }
// }


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...