Trying to drag a button

4 posts, 0 answers
  1. Eric
    Eric avatar
    24 posts
    Member since:
    Sep 2008

    Posted 11 Dec 2008 Link to this post

    I'm trying to put together a simple example. I am tring to drag a button onto one of two grids inside of a larger grid.

    I created a DraggableButton class and a DropTargetGrid class along the lines described in the Draft "Getting Started" document.

    I'm running into a Null Reference error during initialization. The DropTargetGrid seems to initialize, but the DraggableButton chokes and I'm not sure why. It compiles, but chokes on initialization.

    Here is my implementation for the DraggableButton class:

    1 using System;  
    2  
    3 using System.Windows.Controls;  
    4  
    5 using System.Windows.Shapes;  
    6  
    7 using Telerik.Windows;  
    8  
    9 using Telerik.Windows.Controls.DragDrop;  
    10  
    11    
    12  
    13 namespace SimpleDragPOC  
    14  
    15 {  
    16  
    17 public class DraggableButton : Button   
    18  
    19 {  
    20  
    21 public event EventHandler<DragDropQueryEventArgs> DropQuery  
    22  
    23 {  
    24  
    25 add  
    26  
    27 {  
    28  
    29 this.AddHandler(RadDragAndDropManager.DropQueryEvent, value);  
    30  
    31 }  
    32  
    33 remove  
    34  
    35 {  
    36  
    37 this.RemoveHandler(RadDragAndDropManager.DropQueryEvent, value);  
    38  
    39 }  
    40  
    41 }  
    42  
    43 public event EventHandler<DragDropEventArgs> DropInfo  
    44  
    45 {  
    46  
    47 add  
    48  
    49 {  
    50  
    51 this.AddHandler(RadDragAndDropManager.DropInfoEvent, value);  
    52  
    53 }  
    54  
    55 remove  
    56  
    57 {  
    58  
    59 this.RemoveHandler(RadDragAndDropManager.DropInfoEvent, value);  
    60  
    61 }  
    62  
    63 }  
    64  
    65  
    66 public event EventHandler<DragDropQueryEventArgs> DragQuery  
    67  
    68 {  
    69  
    70 add  
    71  
    72 {  
    73  
    74 this.AddHandler(RadDragAndDropManager.DragQueryEvent, value);  
    75  
    76 }  
    77  
    78 remove  
    79  
    80 {  
    81  
    82 this.RemoveHandler(RadDragAndDropManager.DragQueryEvent, value);  
    83  
    84 }  
    85  
    86 }  
    87  
    88 public event EventHandler<DragDropEventArgs> DragInfo  
    89  
    90 {  
    91  
    92 add  
    93  
    94 {  
    95  
    96 this.AddHandler(RadDragAndDropManager.DragInfoEvent, value);  
    97  
    98 }  
    99  
    100 remove  
    101  
    102 {  
    103  
    104 this.RemoveHandler(RadDragAndDropManager.DragInfoEvent, value);  
    105  
    106 }  
    107  
    108 }  
    109  
    110    
    111  
    112 public DraggableButton()  
    113  
    114 {  
    115  
    116 this.DragQuery += new EventHandler<DragDropQueryEventArgs>(DraggableButton_DragQuery);  
    117  
    118 this.DragInfo += new EventHandler<DragDropEventArgs>(DraggableButton_DragInfo);  
    119  
    120 this.DropQuery += new EventHandler<DragDropQueryEventArgs>(DraggableButton_DropQuery);  
    121  
    122 this.DropInfo += new EventHandler<DragDropEventArgs>(DraggableButton_DropInfo);  
    123  
    124 RadDragAndDropManager.SetAllowDrag(thistrue);  
    125  
    126 }  
    127  
    128 void DraggableButton_DropInfo(object sender, DragDropEventArgs e)  
    129  
    130 {  
    131  
    132 throw new NotImplementedException();  
    133  
    134 }  
    135  
    136 void DraggableButton_DropQuery(object sender, DragDropQueryEventArgs e)  
    137  
    138 {  
    139  
    140 throw new NotImplementedException();  
    141  
    142 }  
    143  
    144 void DraggableButton_DragInfo(object sender, DragDropEventArgs e)  
    145  
    146 {  
    147  
    148 // Drag has completed successfully  
    149  
    150 if (e.Options.Status == DragStatus.DragComplete)  
    151  
    152 {  
    153  
    154 e.Handled = true;  
    155  
    156 }  
    157  
    158 }  
    159  
    160 void DraggableButton_DragQuery(object sender, DragDropQueryEventArgs e)  
    161  
    162 {  
    163  
    164 // An object is about the be dragged  
    165  
    166 if (e.Options.Status == DragStatus.DragQuery)  
    167  
    168 {  
    169  
    170 e.Options.DragCue = RadDragAndDropManager.GenerateVisualCue(new Rectangle() { Width = 10, Height = 10 });  
    171  
    172 // Set result to true  
    173  
    174 e.QueryResult = true;  
    175  
    176 e.Options.Payload = this;  
    177  
    178 e.Source = this;  
    179  
    180 e.Handled = true;  
    181  
    182 }  
    183  
    184 // Ask if it is OK to drop the target at the particular location  
    185  
    186 if (e.Options.Status == DragStatus.DropSourceQuery)  
    187  
    188 {  
    189  
    190 e.QueryResult = false;  
    191  
    192 e.Handled = true;  
    193  
    194 }  
    195  
    196 }  
    197  
    198 }  
    199  
    200 }  
    201  


    Any ideas why this blows initialization?

    Specifically this is the error I get:

    {System.Windows.Markup.XamlParseException: System.NullReferenceException: Object reference not set to an instance of an object.
       at Telerik.Windows.Controls.DragDrop.RadDragAndDropManager.Initialize()
       at Telerik.Windows.Controls.DragDrop.RadDragAndDropManager.OnAllowDragChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object newValue, Object oldValue)
       at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet, Boolean isSetByStyle, Boolean isSetByBuiltInStyle)
       at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at Telerik.Windows.Controls.DragDrop.RadDragAndDropManager.SetAllowDrag(DependencyObject obj, Boolean value)
       at SimpleDragPOC.DraggableButton..ctor() [Line: 0 Position: 0]
       --- Inner Exception ---
    Object reference not set to an instance of an object.
     ---> System.NullReferenceException: Object reference not set to an instance of an object.
       at Telerik.Windows.Controls.DragDrop.RadDragAndDropManager.Initialize()
       at Telerik.Windows.Controls.DragDrop.RadDragAndDropManager.OnAllowDragChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object newValue, Object oldValue)
       at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet, Boolean isSetByStyle, Boolean isSetByBuiltInStyle)
       at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at Telerik.Windows.Controls.DragDrop.RadDragAndDropManager.SetAllowDrag(DependencyObject obj, Boolean value)
       at SimpleDragPOC.DraggableButton..ctor()
       --- End of inner exception stack trace ---
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at SimpleDragPOC.Page.InitializeComponent()
       at SimpleDragPOC.Page..ctor()}
  2. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 12 Dec 2008 Link to this post

    Hi Eric,

    The exception arises from the fact that the Initialization of the DragDrop requires the root visual of the application to be present. When you set this in the constructor of the button:

    RadDragAndDropManager.SetAllowDrag(thistrue);  

    it gets called in the constructor of the root Page, so it does not exist yet. It is best if this line is put in the OnApplyTemplate override or the Loaded event handler.

    Also, Unfortunately the Button is one of the controls that captures the mouse on MouseDown. Capturing the mouse means that only the button could ever receive mouse events. These mouse events are handled by the button and not availabe to the DragDrop (or any other control). The limitation is mention in the Online Docs as well:

    http://www.telerik.com/help/silverlight/raddraganddropmanager-structure.html

    Also if you are looking to add drag/drop functionality to controls, it is not necessary to extend them. The attached properties can be set with Styles and the Handlers can be added to any of the roots of the items as well.

    I will be happy to help you with further details or implementation / usage questions.

    Best wishes,
    Miroslav
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. Eric
    Eric avatar
    24 posts
    Member since:
    Sep 2008

    Posted 12 Dec 2008 Link to this post

    I see. That makes sense.

    Actually, I would like to see an example that did work through extending controls. All of the examples I saw worked this way. Can you direct me to an example like that.

    How do I know if the control captures the mouse or not? Is there a list somewhere - that includes the "in-the-box" controls and your controls? That would be helpful.

    -Eric
  5. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 16 Dec 2008 Link to this post

    Hi Eric,

    You can find drag-drop examples in the forums:

    Drag to a panel:
    http://www.telerik.com/ClientsFiles/105201_GridDragDropIdea.zip

    Grid to TreeView:
    http://www.telerik.com/ClientsFiles/109361_DragDropGridToTreeView.zip

    Draft for help for DragDrop:
    http://www.telerik.com/ClientsFiles/109360_RadDragDrop_Help_First_Draft.zip

    Also, there is the CarConfigurator demo which uses DragDrop - you can find it your client.net account, more iformation is available here:

    http://blogs.telerik.com/valentinstoychev/posts/08-11-22/Silverlight_Samples_source_code_available.aspx

    I am sorry that currently there are fewer examples, we are currently working on them. You are welcome to suggest examples that you will like to see.

    There is no list of controls that capture the mouse, this depends on the implementation. For the default controls: As far as I know all ButtonBase descendants capture the mouse, but this may not be exhaustive. I will try to create a list with the default controls that do this - thanks for the suggestion.

    Best wishes,
    Miroslav
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top