DragDropManager doesn't drop on all control

2 posts, 0 answers
  1. CLAUDIO ROSSI
    CLAUDIO ROSSI avatar
    4 posts
    Member since:
    May 2010

    Posted 12 Mar 2012 Link to this post

    I am using new DragDropManager  and all works fine less then for a little problem that with previous version (RadDragDropManager) was working correctly :


    Like attached picture , i have one user control composed by one Grid with one Image and one TextBox ; the usercontrol is marked with :

    <UserControl x:Class="LibreriaWpfCoster.EditorSingolaVariabileInt32"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     
                 xmlns:globalization="clr-namespace:System.Globalization;assembly=mscorlib"
                 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
                  
                 MinWidth="40"
                 MinHeight="20"
                  
                 mc:Ignorable="d"
                 d:DesignWidth="40"
                 d:DesignHeight="71"
                 Loaded="UserControl_Loaded"
                 >
         
        <Grid x:Name="LayoutRoot"
              Background="LightGreen"
              AllowDrop="True"
              >
     
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="20"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
     
            <!--Icona che cambia in funzione del tipo rilevato e cliccandoci sopra resetta la variabile-->
            <Image x:Name="imgObj"
                   Source="Images/Numero32Negativo.png"
                   Stretch="Uniform"
                   MouseLeftButtonDown="imgObj_MouseLeftButtonDown"
                   />
     
            <!--Oggetto per editare il nome della variabile-->
            <TextBox x:Name="varObj" 
                     Grid.Column="1"
                     Panel.ZIndex="0"
                     Text="{Binding Path=PlcVar, Mode=TwoWay}"
                     PreviewKeyDown="varObj_PreviewKeyDown"
                     ContextMenu="{x:Null}"
                     />
     
            <!--Oggetto per editare il nome della SysVar-->
            <TextBox x:Name="varSysObj" 
                     Grid.Column="1"
                     Panel.ZIndex="0"
                     Text="{Binding Path=SysVarName, Mode=TwoWay}"
                     PreviewKeyDown="varObj_PreviewKeyDown"
                     ContextMenu="{x:Null}"
                     />
     
        </Grid>
    </UserControl>

    AllowDrop is placed on the Grid , but placing on the entire Usercontrol doesn't change the problem.

    When i drop with this code :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
     
    using ClassiCoster;
    using System.ComponentModel;
     
    using Telerik.Windows.DragDrop;
    using Telerik.Windows;
     
    namespace LibreriaWpfCoster
    {
        /// <summary>
        /// Interaction logic for EditorSingolaVariabileInt16.xaml
        /// </summary>
        public partial class EditorSingolaVariabileInt32 : UserControl
        {
            public EditorSingolaVariabileInt32()
            {
                InitializeComponent();
     
                DragDropManager.AddDropHandler(this, OnVarDrop);
                DragDropManager.AddDragOverHandler(this, OnVarDragOver);
                DragDropManager.AddDragLeaveHandler(this, OnVarDragLeave);
            }
             
            /// <summary>
            /// Variabile PLC
            /// </summary>
            public SingolaVariabileInt32 Variabile
            {
                get { return (SingolaVariabileInt32)GetValue(VariabileProperty); }
                set { SetValue(VariabileProperty, value); }
            }
     
            public static readonly DependencyProperty VariabileProperty = DependencyProperty.Register("Variabile",
                                                                                                      typeof(SingolaVariabileInt32),
                                                                                                      typeof(EditorSingolaVariabileInt32),
                                                                                                      new PropertyMetadata(new SingolaVariabileInt32(), OnVariabilePropertyChanged));
     
     
            private static void OnVariabilePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                (d as EditorSingolaVariabileInt32).LayoutRoot.DataContext = ((EditorSingolaVariabileInt32)d).Variabile;
                (d as EditorSingolaVariabileInt32).MettiInPrimoPianoIlControlloCorretto();
            }
     
            private static List<PlcVariableType> _allowedVars = new List<PlcVariableType>(new PlcVariableType[]
            {
                PlcVariableType.INT8,
                PlcVariableType.UINT8,
                PlcVariableType.INT16,
                PlcVariableType.UINT16,
                PlcVariableType.INT32,
                PlcVariableType.UINT32,
            });
     
            /// <summary>
            /// Specifica i tipi di variabili ammessi per venir messe in binding
            /// </summary>
            public List<PlcVariableType> AllowedVars
            {
                get { return _allowedVars; }
                set { _allowedVars = value; }
            }
     
            private static List<PlcVariableType> _allowedIndexVars = new List<PlcVariableType>(new PlcVariableType[]
            {
                 PlcVariableType.INT8,
                 PlcVariableType.UINT8,
                 PlcVariableType.INT16,
                 PlcVariableType.UINT16,
                 PlcVariableType.INT32,
                 PlcVariableType.UINT32,
            });
     
            /// <summary>
            /// Specifica i tipi di variabili che possono venir messe in binding per gli indici
            /// </summary>
            public List<PlcVariableType> AllowedIndexVars
            {
                get { return _allowedIndexVars; }
                set { _allowedIndexVars = value; }
            }
     
            /// <summary>
            /// Fa l'effetto per draggare una variabile
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void OnVarDragOver(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
            {
                Boolean Evidenzia = false;
     
                if (e.Data is DataObject)
                {
                    VariabilePlc vPlc = (e.Data as DataObject).GetData(typeof(VariabilePlc)) as VariabilePlc;
     
                    if (vPlc != null)
                    {
                        if (vPlc.SysVarID > 0) ProvaASelezionareUnIndice();
     
                        Evidenzia = GenericiEditorVariabili.DimmiSeAppartieneAllaLista(_allowedVars, vPlc.Tipo);
     
                        if (!Evidenzia && vPlc.SysVarID < 1 && GenericiEditorVariabili.DimmiSeHoSelezionatoUnIndice(varObj))
                        {
                            Evidenzia = GenericiEditorVariabili.DimmiSeAppartieneAllaLista(_allowedIndexVars, vPlc.Tipo);
                        }
                    }
                }
     
                e.Effects = Evidenzia ? DragDropEffects.Copy : DragDropEffects.None;
     
                GenericiEditorVariabili.EvidenziaPerDrag(LayoutRoot, Evidenzia);
     
                e.Handled = true;
            }
     
            private void OnVarDragLeave(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
            {
                e.Effects = DragDropEffects.None;
                GenericiEditorVariabili.EvidenziaPerDrag(LayoutRoot, false);
                e.Handled = true;
            }
     
            /// <summary>
            /// Viene eseguito al drop di una variabile
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void OnVarDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
            {
                GenericiEditorVariabili.EvidenziaPerDrag(LayoutRoot, false);
                if (e.Data == null || !(e.Data is DataObject)) return;
     
                VariabilePlc vPlc = (e.Data as DataObject).GetData(typeof(VariabilePlc)) as VariabilePlc;
     
                if (vPlc != null)
                {
                    ProvaASelezionareUnIndice();
     
                    if (vPlc.SysVarID < 1 && GenericiEditorVariabili.DimmiSeHoSelezionatoUnIndice(varObj))
                    {
                        if (GenericiEditorVariabili.DimmiSeAppartieneAllaLista(_allowedIndexVars, vPlc.Tipo))
                        {
                            String strPrima = varObj.Text.Substring(0, varObj.SelectionStart);
                            String strDopo = varObj.Text.Substring(varObj.SelectionStart + varObj.SelectionLength);
                            Variabile.PlcVar = strPrima + vPlc.PlcVar + strDopo;
                        }
                        else
                        {
                            if (GenericiEditorVariabili.DimmiSeAppartieneAllaLista(_allowedVars, vPlc.Tipo))
                            {
                                Variabile.ResetVar();
                                Variabile.PlcVar = vPlc.PlcVar;
                            }
                        }
                    }
                    else
                    {
                        if (GenericiEditorVariabili.DimmiSeAppartieneAllaLista(_allowedVars, vPlc.Tipo))
                        {
                            Variabile.ResetVar();
     
                            if (vPlc.SysVarID > 0)
                            {
                                Variabile.SysVarName = vPlc.PlcVar;
                                Variabile.SysVarID = vPlc.SysVarID;
                            }
                            else
                            {
                                Variabile.PlcVar = vPlc.PlcVar;
                            }
                        }
                    }
     
                    MettiInPrimoPianoIlControlloCorretto();
                    e.Handled = true;
                }
            }
     
     
     
            //private void OnDropQuery(object sender, DragDropQueryEventArgs e)
            //{
            //    switch (e.Options.Status)
            //    {
            //        case DragStatus.DropDestinationQuery:
            //            if (e.Options.Payload is VariabilePlc)
            //            {
            //                VariabilePlc vPlc = e.Options.Payload as VariabilePlc;
     
            //                if (GenericiEditorVariabili.DimmiSeAppartieneAllaLista(_allowedVars, vPlc.Tipo) ||
            //                    (vPlc.SysVarID < 1 && GenericiEditorVariabili.DimmiSeAppartieneAllaLista(_allowedIndexVars, vPlc.Tipo))
            //                   )
            //                {
            //                    e.QueryResult = true;
            //                    e.Handled = true;
            //                }
            //            }
            //            break;
            //    }
            //}
     
            //private void OnDropInfo(object sender, DragDropEventArgs e)
            //{
            //    LibreriaWpfCoster.CustomDragCue cueObj = e.Options.DragCue as LibreriaWpfCoster.CustomDragCue;
     
            //    if (e.Options.Status == DragStatus.DropPossible) GenericiEditorVariabili.EvidenziaPerDrag(LayoutRoot,cueObj , true);
            //    if (e.Options.Status == DragStatus.DropImpossible) GenericiEditorVariabili.EvidenziaPerDrag(LayoutRoot,cueObj , false);
     
            //    if (e.Options.Status != DragStatus.DropComplete) return;
     
            //    GenericiEditorVariabili.EvidenziaPerDrag(LayoutRoot,cueObj, false);
     
            //    VariabilePlc vPlc = e.Options.Payload as VariabilePlc;
     
            //    if (e.Options.Status == DragStatus.DropComplete && vPlc != null)
            //    {
            //        ProvaASelezionareUnIndice();
     
            //        if (vPlc.SysVarID < 1 && GenericiEditorVariabili.DimmiSeHoSelezionatoUnIndice(varObj))
            //        {
            //            if (GenericiEditorVariabili.DimmiSeAppartieneAllaLista(_allowedIndexVars, vPlc.Tipo))
            //            {
            //                String strPrima = varObj.Text.Substring(0, varObj.SelectionStart);
            //                String strDopo = varObj.Text.Substring(varObj.SelectionStart + varObj.SelectionLength);
            //                Variabile.PlcVar = strPrima + vPlc.PlcVar + strDopo;
            //            }
            //            else
            //            {
            //                if (GenericiEditorVariabili.DimmiSeAppartieneAllaLista(_allowedVars, vPlc.Tipo))
            //                {
            //                    Variabile.ResetVar();
            //                    Variabile.PlcVar = vPlc.PlcVar;
            //                }
            //            }
            //        }
            //        else
            //        {
            //            if (GenericiEditorVariabili.DimmiSeAppartieneAllaLista(_allowedVars, vPlc.Tipo))
            //            {
            //                Variabile.ResetVar();
     
            //                if (vPlc.SysVarID > 0)
            //                {
            //                    Variabile.SysVarName = vPlc.PlcVar;
            //                    Variabile.SysVarID = vPlc.SysVarID;
            //                }
            //                else
            //                {
            //                    Variabile.PlcVar = vPlc.PlcVar;
            //                }
            //            }
            //        }
     
            //        MettiInPrimoPianoIlControlloCorretto();
            //        e.Handled = true;
            //    }
            //}
     
            /// <summary>
            /// Partendo dalla posizione del cursore prova a vedere se nell'intorno c'è un array
            /// </summary>
            private void ProvaASelezionareUnIndice()
            {
                int PosParentesiSinistra, PosParentesiDestra;
     
                if (varObj.Text.Length < 4 || varObj.SelectionStart < 1) return;
                if (varObj.SelectionStart >= varObj.Text.Length) return;
     
                if (",".IndexOf(varObj.Text.Substring(varObj.SelectionStart, 1)) != -1) varObj.SelectionStart++;
                for (PosParentesiSinistra = varObj.SelectionStart; PosParentesiSinistra > 1; PosParentesiSinistra--)
                {
                    if ("[,".IndexOf(varObj.Text.Substring(PosParentesiSinistra, 1)) != -1)
                    {
                        break;
                    }
                }
     
                for (PosParentesiDestra = varObj.SelectionStart; PosParentesiDestra < varObj.Text.Length; PosParentesiDestra++)
                {
                    if (",]".IndexOf(varObj.Text.Substring(PosParentesiDestra, 1)) != -1)
                    {
                        break;
                    }
                }
     
                if (PosParentesiSinistra > 0 && (PosParentesiDestra < varObj.Text.Length))
                {
                    varObj.SelectionStart = PosParentesiSinistra + 1;
                    varObj.SelectionLength = PosParentesiDestra - PosParentesiSinistra - 1;
                }
            }
     
            private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
                LayoutRoot.DataContext = Variabile;
                MettiInPrimoPianoIlControlloCorretto();
                varObj.ContextMenu = null;
            }
     
            private void MettiInPrimoPianoIlControlloCorretto()
            {
                Int32 varObjZ = 0;
                Int32 varSysObjZ = 0;
     
                if (Variabile.SysVarID > 0)
                {
                    varSysObjZ = 1;
                }
                else
                {
                    varObjZ = 1;
                }
     
                varObj.SetValue(Panel.ZIndexProperty, varObjZ);
                varSysObj.SetValue(Panel.ZIndexProperty, varSysObjZ);
            }
     
            /// <summary>
            /// Ho gestito quest'evento per aggiornare in caso di testo vuoto
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void varObj_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                switch (e.Key)
                {
                    case Key.Enter:
                    case Key.Back:
                    case Key.Delete:
                        Variabile.ResetVar();
                        MettiInPrimoPianoIlControlloCorretto();
                        break;
     
                    default:
                        break;
                }
     
                e.Handled = true;
            }
     
            /// <summary>
            /// Serve per cancellare la variabile plc se gli si clicca sopra
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void imgObj_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                Variabile.ResetVar();
                MettiInPrimoPianoIlControlloCorretto();
                e.Handled = true;
            }
     
        }
    }

    The drop is allowed only on the Image of the control and not on the whoole usercontrol loke with previous version ....
    I have try too setting AllowDrop on only one RadNumericUpDown and in this case the drop is allowed ONLY if i am with cursor over the up/down buttons

    I have added two images to explain the problem.
    Can you help me , because for my company is important to solve this problem.

    Best regards

    Claudio Rossi
  2. Nick
    Admin
    Nick avatar
    593 posts

    Posted 14 Mar 2012 Link to this post

    Hello Claudio,

    Unfortunately, this is a framework related problem. The TextBlock won't allow drop unless the DataObject that is being dragged over it contains content of type Text. You can use the DragInitialize event to set the necessary data.

    private void OnDragInitialize(object sender, DragInitializeEventArgs e)
            {
                e.AllowedEffects = DragDropEffects.All;
                var data = ((FrameworkElement)e.OriginalSource).DataContext;
                e.DragVisual = new DragVisual()
                {
                    Content = data,
                    ContentTemplate = this.Resources["DragTemplate"] as DataTemplate
                };
     
                DataObject dataObject = new DataObject();
                dataObject.SetText(data.ToString());
                dataObject.SetData("DragData", data);
                dataObject.SetData("Text", data.ToString());
     
                e.Data = dataObject;
            }

    You can later extract the data you need with the GetData method of the DataObject.

    Since the TextBox handles the drop on its own, if you need to handle some specific logic in the drop event handler, you can subscribe to the Drop event with "HandledEventsToo" set to true, or you can use the PreviewDrop event.
    DragDropManager.AddDropHandler(this.MyControl, OnMyControlDrop, true);

    I am attaching the project I used to test the scenario, for your reference.

    Hope this helps! 

    Greetings,
    Nik
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top