This is a migrated thread and some comments may be shown as answers.

ControlExtensions static class

3 Answers 62 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
tomas
Top achievements
Rank 1
tomas asked on 09 Dec 2010, 04:51 PM
Hello Telerik.

I am afraid, I am really stupid, but I don't know how is it possible to use ControlExtensions static method FindItemsControlParent on
private void OnDragInfo( object sender, DragDropEventArgs e )
{
   if ( e.Options.Status == DragStatus.DragComplete )
   {
       var listBox = e.Options.Source.FindItemsConrolParent() as ItemsControl;
       var itemsSource = listBox.ItemsSource as IList;
       var operation = e.Options.Payload as DragDropOperation;
       itemsSource.Remove( operation.Payload );
   }

ControlExtensions class is defined like that:
public static class ControlExtensions
{
   public static ItemsControl FindItemsConrolParent( this FrameworkElement target )
   {
       ItemsControl result = null;
       result = target.Parent as ItemsControl;
       if ( result != null )
           return result;
       result = ItemsControl.ItemsControlFromItemContainer( target );
       if ( result != null )
           return result;
       return FindVisualParent<ItemsControl>( target );
   }
   public static T FindVisualParent<T>( FrameworkElement target ) where T : FrameworkElement
   {
       if ( target == null )
           return null;
       var visParent = VisualTreeHelper.GetParent( target );
       var result = visParent as T;
       if ( result != null )
           return result;
       return FindVisualParent<T>( visParent as FrameworkElement );
   }
   public static T GetTemplateChild<T>( this Control target, string templtePartName ) where T : FrameworkElement
   {
       if ( target == null )
           throw new ArgumentNullException( "target", "Cannot get the templated child of a null object" );
       var childCount = VisualTreeHelper.GetChildrenCount( target );
       if ( childCount == 0 )
           return null;
       return ( VisualTreeHelper.GetChild( target, 0 ) as FrameworkElement ).FindName( templtePartName ) as T;
   }
}

Everything is from your Drag and Drop Between ListBoxes with user feedback demo project.

My second question is why do you set the selectedIndex of the itemsSource using the dispatcher. There is no such necesity described in MS help files about SelectedIndex property...

Thank you for any clarification.

I think that it would be a little bit better to signalize the possibility to drop dragged item by directly showing inserted grayed copy of dragged item then your dropCueElement. Something like IPHONE animated panel.  

Tom

3 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 10 Dec 2010, 04:28 PM
Hi tomas,

To use the extension methods you will have to reference the namespace in which the extensions are defined. For example, if the ControlExtensions class is defined in MyExtensions namespace you will have to include the following line in the file where you will be using the extensions:

"using MyExtensions;"

About the Dispatcher, naturally there shouldn't be a reason to set SelectedIndex using a Dispatcher. You could try removing the dispatcher. 

Regards,
Milan
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
tomas
Top achievements
Rank 1
answered on 10 Dec 2010, 04:36 PM
Hello Milan.

Thank you for your reply.

Regarding to ControlExtensions class: It is working for me. The problem is, that I don't know why. How is it possible that some class (ControlExtenesions) is able to add new methods to another class (UIElement). This is what I would like to know.

Thank you very much.

Tom
0
tomas
Top achievements
Rank 1
answered on 15 Dec 2010, 02:24 PM
Ok.

I have found this: http://msdn.microsoft.com/en-us/library/bb383977.aspx

I have already understand the mechanism of extenesion methods.

Tom
Tags
DragAndDrop
Asked by
tomas
Top achievements
Rank 1
Answers by
Milan
Telerik team
tomas
Top achievements
Rank 1
Share this question
or