locked
How do I retrieve objects that were dragged onto a canvas panel control. RRS feed

  • Question

  • Josh Smith had a great article Stream YouTube Videos In WPF on searching an RSS feed and adding 'youtube' info objects to a Canvas as per this condensed method:

            private void PopulateCanvas(List<YouTubeInfo> infos)
            {
                for (int i = 0; i < infos.Count; i++)
                {
                    YouTubeResultControl control = new YouTubeResultControl { Info = infos[i] };
                    dragCanvas.Children.Add(control);
                }
            }

    How would I fetch the controls added in PopulateCanvas above and add them to this Carousel control?

            private void buttonCarouselize_Click(object sender, RoutedEventArgs e)
            {
                for (int i = 0; i < this.dragCanvas.Children.Count ; i++)   
                {
                    this.dragCanvas.Children.?? ;
                    this.xamDataCarousel1.DataItems.Add(this.dragCanvas.Children(??) ;
                }

            }

    Thank you.  Greg


    Wednesday, July 2, 2008 11:16 PM

Answers

  • FOREACH is much neater than FOR if you are iterating a whole list:

     
    private void PopulateCanvas(List<YouTubeInfo> infos)  
            {  
                foreach ( YouTubeInfo info in infos)  
                {  
                    dragCanvas.Children.Add(new YouTubeResultControl( info ));  
                }  
            }  
     
     private void buttonCarouselize_Click(object sender, RoutedEventArgs e)  
            {  
       // Assumes all child controls are YouTubeInfo  
                foreach ( YouTubeInfo info in this.dragCanvas.Children)      
                {  
                    this.xamDataCarousel1.DataItems.Add(info) ;  
                }  
    // If all child controls are NOT YouTubeInfo  
                foreach ( UIElement info in this.dragCanvas.Children)      
                {  
                    if ( info is YouTubeInfo )  
                    {  
                        this.xamDataCarousel1.DataItems.Add(info) ;  
                    }  
                }  
     
            }  
     
    Do you mean to add a YouTubeResultControl into dragCanvas but remove them as YouTubeInfo?
    • Marked as answer by hazz Thursday, July 3, 2008 2:07 PM
    Thursday, July 3, 2008 1:11 PM

All replies

  • FOREACH is much neater than FOR if you are iterating a whole list:

     
    private void PopulateCanvas(List<YouTubeInfo> infos)  
            {  
                foreach ( YouTubeInfo info in infos)  
                {  
                    dragCanvas.Children.Add(new YouTubeResultControl( info ));  
                }  
            }  
     
     private void buttonCarouselize_Click(object sender, RoutedEventArgs e)  
            {  
       // Assumes all child controls are YouTubeInfo  
                foreach ( YouTubeInfo info in this.dragCanvas.Children)      
                {  
                    this.xamDataCarousel1.DataItems.Add(info) ;  
                }  
    // If all child controls are NOT YouTubeInfo  
                foreach ( UIElement info in this.dragCanvas.Children)      
                {  
                    if ( info is YouTubeInfo )  
                    {  
                        this.xamDataCarousel1.DataItems.Add(info) ;  
                    }  
                }  
     
            }  
     
    Do you mean to add a YouTubeResultControl into dragCanvas but remove them as YouTubeInfo?
    • Marked as answer by hazz Thursday, July 3, 2008 2:07 PM
    Thursday, July 3, 2008 1:11 PM
  •  Thanks Lukos!
     
    • Edited by hazz Thursday, July 3, 2008 2:33 PM Updated reply.
    Thursday, July 3, 2008 2:18 PM