Пример #1
0
        private static void OnOnDropCommandChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
        {
            var attached = DragDropManager.GetIsDropTargetAttached( d );
            if( !attached )
            {
                DragDropManager.SetIsDropTargetAttached( d, true );

                var ui = ( UIElement )d;

                ui.AllowDrop = true;
                var ctrl = ui as Control;
                if( ctrl != null )
                {
                    var bkg = ctrl.GetValue( ItemsControl.BackgroundProperty );
                    if( bkg == null )
                    {
                        ctrl.SetValue( ItemsControl.BackgroundProperty, Brushes.Transparent );
                    }
                }

                ui.DragEnter += ( s, args ) =>
                {
                    var os = ( DependencyObject )args.OriginalSource;
                    var command = DragDropManager.FindDragEnterCommand( os );
                    if( command != null )
                    {
                        var dropTarget = DragDropManager.FindDropTarget( os );
                        var cmdArgs = new DragEnterArgs(
                            args.Data,
                            args.KeyStates,
                            dropTarget,
                            args.AllowedEffects );

                        if( command.CanExecute( cmdArgs ) )
                        {
                            command.Execute( cmdArgs );
                        }
                    }
                };

                ui.DragLeave += ( s, args ) =>
                {
                    var os = ( DependencyObject )args.OriginalSource;
                    var command = DragDropManager.FindDragLeaveCommand( os );
                    if( command != null )
                    {
                        var dropTarget = DragDropManager.FindDropTarget( os );
                        var cmdArgs = new DragLeaveArgs(
                            args.Data,
                            args.KeyStates,
                            dropTarget,
                            args.AllowedEffects );

                        if( command.CanExecute( cmdArgs ) )
                        {
                            command.Execute( cmdArgs );
                        }
                    }
                };

                ui.DragOver += ( s, args ) =>
                {
                    var os = ( DependencyObject )args.OriginalSource;

                    var command = DragDropManager.FindDropCommand( os );
                    if( command != null )
                    {
                        var dropTarget = DragDropManager.FindDropTarget( os );

                        Point position = new Point( 0, 0 );
                        if( os is IInputElement )
                        {
                            position = args.GetPosition( ( IInputElement )os );
                        }

                        var cmdArgs = new DragOverArgs(
                            args.Data,
                            args.KeyStates,
                            dropTarget,
                            args.AllowedEffects,
                            position );

                        var result = command.CanExecute( cmdArgs );
                        if( !result )
                        {
                            args.Effects = cmdArgs.Effects;
                            args.Handled = true;
                        }
                    }
                    else
                    {
                        args.Effects = args.AllowedEffects;
                        args.Handled = true;
                    }
                };

                ui.Drop += ( s, args ) =>
                {
                    var os = ( DependencyObject )args.OriginalSource;

                    var command = DragDropManager.FindDropCommand( os );
                    if( command != null )
                    {
                        var dropTarget = DragDropManager.FindDropTarget( os );

                        Point position = new Point( 0, 0 );
                        if( os is IInputElement )
                        {
                            position = args.GetPosition( ( IInputElement )os );
                        }

                        var cmdArgs = new DropArgs( args.Data, args.KeyStates, dropTarget, position );
                        command.Execute( cmdArgs );
                    }
                };
            }
        }
Пример #2
0
        private static void OnOnDropCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var attached = DragDropManager.GetIsDropTargetAttached(d);

            if (!attached)
            {
                DragDropManager.SetIsDropTargetAttached(d, true);

                var ui = ( UIElement )d;

                ui.AllowDrop = true;
                var ctrl = ui as Control;
                if (ctrl != null)
                {
                    var bkg = ctrl.GetValue(ItemsControl.BackgroundProperty);
                    if (bkg == null)
                    {
                        ctrl.SetValue(ItemsControl.BackgroundProperty, Brushes.Transparent);
                    }
                }

                ui.DragEnter += (s, args) =>
                {
                    var os      = ( DependencyObject )args.OriginalSource;
                    var command = DragDropManager.FindDragEnterCommand(os);
                    if (command != null)
                    {
                        var dropTarget = DragDropManager.FindDropTarget(os);
                        var cmdArgs    = new DragEnterArgs(
                            args.Data,
                            args.KeyStates,
                            dropTarget,
                            args.AllowedEffects);

                        if (command.CanExecute(cmdArgs))
                        {
                            command.Execute(cmdArgs);
                        }
                    }
                };

                ui.DragLeave += (s, args) =>
                {
                    var os      = ( DependencyObject )args.OriginalSource;
                    var command = DragDropManager.FindDragLeaveCommand(os);
                    if (command != null)
                    {
                        var dropTarget = DragDropManager.FindDropTarget(os);
                        var cmdArgs    = new DragLeaveArgs(
                            args.Data,
                            args.KeyStates,
                            dropTarget,
                            args.AllowedEffects);

                        if (command.CanExecute(cmdArgs))
                        {
                            command.Execute(cmdArgs);
                        }
                    }
                };

                ui.DragOver += (s, args) =>
                {
                    var os = ( DependencyObject )args.OriginalSource;

                    var command = DragDropManager.FindDropCommand(os);
                    if (command != null)
                    {
                        var dropTarget = DragDropManager.FindDropTarget(os);

                        Point position = new Point(0, 0);
                        if (os is IInputElement)
                        {
                            position = args.GetPosition(( IInputElement )os);
                        }

                        var cmdArgs = new DragOverArgs(
                            args.Data,
                            args.KeyStates,
                            dropTarget,
                            args.AllowedEffects,
                            position);

                        var result = command.CanExecute(cmdArgs);
                        if (!result)
                        {
                            args.Effects = cmdArgs.Effects;
                            args.Handled = true;
                        }
                    }
                    else
                    {
                        args.Effects = args.AllowedEffects;
                        args.Handled = true;
                    }
                };

                ui.Drop += (s, args) =>
                {
                    var os = ( DependencyObject )args.OriginalSource;

                    var command = DragDropManager.FindDropCommand(os);
                    if (command != null)
                    {
                        var dropTarget = DragDropManager.FindDropTarget(os);

                        Point position = new Point(0, 0);
                        if (os is IInputElement)
                        {
                            position = args.GetPosition(( IInputElement )os);
                        }

                        var cmdArgs = new DropArgs(args.Data, args.KeyStates, dropTarget, position);
                        command.Execute(cmdArgs);
                    }
                };
            }
        }