如何根据拖动源和拖放目标允许拖放并反映在游标类型中

时间:2018-06-14 14:08:31

标签: c# wpf drag-and-drop

我有一个有四个ListBox的应用程序。我想只允许将项目从第一个ListBox拖动到第三个ListBox,从第二个ListBox拖动到第四个ListBox。我还希望光标适当更改以反映允许和不允许的内容。即不允许删除时显示Cursor.No,允许删除时显示Cursor.Cross。

我认为我允许/不允许拖放部分。我不确定如何实现更改游标类型。我应该如何修改我的代码来实现这些东西?

的Xaml:

<Window x:Name="window" x:Class="testApp.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:local="clr-namespace:testApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="800" DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}" Background="White">
    <StackPanel Margin="0">
        <Button Content="Add" HorizontalAlignment="Left" Height="24" Margin="5,5,0,0" Width="100" Click="Button_Click"/>
        <Grid Height="500" Margin="5,5,5,0" VerticalAlignment="Top">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <ListBox Margin="0,0,5,5" ItemsSource="{Binding MyItems, ElementName=window}" Background="White" PreviewMouseLeftButtonDown="BeginDrag" Drop="DropData" AllowDrop="True" Tag="{Binding Box1Tag, ElementName=window, Mode=OneWay}"/>
            <ListBox Grid.Column="1" Margin="5,0,0,5" ItemsSource="{Binding SecondItems, ElementName=window}" Drop="DropData" PreviewMouseLeftButtonDown="BeginDrag" AllowDrop="True" Tag="{Binding Box2Tag, ElementName=window, Mode=OneWay}"/>
            <ListBox Margin="0,0,5,5" ItemsSource="{Binding ThirdItems, ElementName=window}" Background="White" Grid.Row="1" PreviewMouseLeftButtonDown="BeginDrag" Drop="DropData" AllowDrop="True" Tag="{Binding Box3Tag, ElementName=window, Mode=OneWay}"/>
            <ListBox Margin="5,0,0,5" ItemsSource="{Binding FourthItems, ElementName=window}" Background="White" Grid.Row="1" Grid.Column="1" Drop="DropData" PreviewMouseLeftButtonDown="BeginDrag" AllowDrop="True" Tag="{Binding Box4Tag, ElementName=window, Mode=OneWay}"/>
        </Grid>
    </StackPanel>
</Window>

代码背后:

namespace testApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Random random = new Random();

        public MainWindow()
        {
            MyItems = new ObservableCollection<int>();
            SecondItems = new ObservableCollection<int>();
            ThirdItems = new ObservableCollection<int>();
            FourthItems = new ObservableCollection<int>();
            InitializeComponent();
        }

        public string Box1Tag => "Box1";

        public string Box2Tag => "Box2";

        public string Box3Tag => "Box3";

        public string Box4Tag => "Box4";

        public ObservableCollection<int> MyItems { get; private set; }

        public ObservableCollection<int> SecondItems { get; private set; }

        public ObservableCollection<int> ThirdItems { get; private set; }

        public ObservableCollection<int> FourthItems { get; private set; }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MyItems.Add(random.Next());
            SecondItems.Add(random.Next());
        }

        private void BeginDrag(object sender, MouseButtonEventArgs e)
        {
            if (sender is ListBox)
            {
                int? draggedItem = (int?)((ListBox)sender).SelectedItem;
                string tag = (string)((ListBox)sender).Tag;

                if (draggedItem != null)
                {
                    DataObject data = new DataObject();
                    data.SetData("int", draggedItem);
                    data.SetData("string", tag);
                    DragDrop.DoDragDrop((ListBox)sender, data, DragDropEffects.Move);
                }
            }
        }

        private void DropData(object sender, DragEventArgs e)
        {
            if (sender is ListBox)
            {
                string dragsource = (string)e.Data.GetData("string");
                string droptarget = (string)((ListBox)(sender)).Tag;
                int item = (int)e.Data.GetData("int");

                if (dragsource == Box1Tag && droptarget == Box3Tag)
                {
                    MyItems.Remove(item);
                    ThirdItems.Add(item);
                }
                else if (dragsource == Box2Tag && droptarget == Box4Tag)
                {
                    SecondItems.Remove(item);
                    FourthItems.Add(item);
                }
            }
        }
    }
}

0 个答案:

没有答案