ControlExtensions static class

4 posts, 0 answers
  1. tomas
    tomas avatar
    139 posts
    Member since:
    Sep 2012

    Posted 09 Dec 2010 Link to this post

    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
  2. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 10 Dec 2010 Link to this post

    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
  3. DevCraft banner
  4. tomas
    tomas avatar
    139 posts
    Member since:
    Sep 2012

    Posted 10 Dec 2010 Link to this post

    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
  5. tomas
    tomas avatar
    139 posts
    Member since:
    Sep 2012

    Posted 15 Dec 2010 Link to this post

    Ok.

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

    I have already understand the mechanism of extenesion methods.

    Tom
Back to Top