locked
How to get an element at a specific position from wrap panel? RRS feed

  • Question

  • I've few elements in the wrap panel..on tapping on one element should add border around it and remove border of other elements of wrap panel.

    How to do it?

    Here is the code

            public void AddThumbnail(int LayerNo, IBuffer Buffer)
            {
                var bitmap = new WriteableBitmap(0, 0);
                bitmap.SetSource(Buffer.AsStream());
    
                Layer layer = new Layer()
                {
                    Bitmap = bitmap,
                    Text = "Layer " + LayerNo.ToString(),
                    Width = 80,
                    Margin = new Thickness(6)
                };
    
                layer.Tap += (object sender, System.Windows.Input.GestureEventArgs e) =>
                {
                    layer.BorderBrush = new SolidColorBrush(Colors.Cyan);
                    layer.BorderThickness = new Thickness(2, 2, 2, 2);
                };
    
                LayersPanel.Children.Add(layer);
                bitmap = null;
            }


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Wednesday, October 1, 2014 4:08 AM

Answers

  • I quite understand your approach.  I did it this way:

    <toolkit:WrapPanel x:Name="MyWrapPanel" Width="400" Height="600" Background="White">
      <Image Source="Images/fish.png" Tap="Image_Tap" Width="150" Margin="10" />
      <Image Source="Images/kitten.jpg" Tap="Image_Tap" Width="150" Margin="10"/>
      <Image Source="Images/skull.png" Tap="Image_Tap" Width="150" Margin="10"/>
    </toolkit:WrapPanel>

    private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
      int ChildIndex = 0;
      UIElement FindElement = null;
      Border FindBorder = null;
      UIElement Current = sender as UIElement;
      foreach (Object CurrentObject in MyWrapPanel.Children)
      {
        if (CurrentObject.GetType().ToString() == "System.Windows.Controls.Border")
          {
            FindBorder = CurrentObject as Border;
            FindElement = FindBorder.Child;
            if (FindElement == Current) return;
            break;
          }
          ChildIndex++;
        }
        if (FindElement != null && FindBorder != null)
        {
          FindBorder.Child = null;
          MyWrapPanel.Children.RemoveAt(ChildIndex);
          MyWrapPanel.Children.Insert(ChildIndex, FindElement);
        }
    
        int MyIndex = MyWrapPanel.Children.IndexOf(Current);
        MyWrapPanel.Children.Remove(Current);
        Border NewBorder = new Border();
        NewBorder.Child = Current;
        NewBorder.BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue);
         NewBorder.BorderThickness = new Thickness(1);
         MyWrapPanel.Children.Insert(MyIndex, NewBorder);
        }
      }
    }
    


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by venu238 Monday, October 6, 2014 10:55 AM
    Wednesday, October 1, 2014 2:48 PM

All replies

  • I quite understand your approach.  I did it this way:

    <toolkit:WrapPanel x:Name="MyWrapPanel" Width="400" Height="600" Background="White">
      <Image Source="Images/fish.png" Tap="Image_Tap" Width="150" Margin="10" />
      <Image Source="Images/kitten.jpg" Tap="Image_Tap" Width="150" Margin="10"/>
      <Image Source="Images/skull.png" Tap="Image_Tap" Width="150" Margin="10"/>
    </toolkit:WrapPanel>

    private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
      int ChildIndex = 0;
      UIElement FindElement = null;
      Border FindBorder = null;
      UIElement Current = sender as UIElement;
      foreach (Object CurrentObject in MyWrapPanel.Children)
      {
        if (CurrentObject.GetType().ToString() == "System.Windows.Controls.Border")
          {
            FindBorder = CurrentObject as Border;
            FindElement = FindBorder.Child;
            if (FindElement == Current) return;
            break;
          }
          ChildIndex++;
        }
        if (FindElement != null && FindBorder != null)
        {
          FindBorder.Child = null;
          MyWrapPanel.Children.RemoveAt(ChildIndex);
          MyWrapPanel.Children.Insert(ChildIndex, FindElement);
        }
    
        int MyIndex = MyWrapPanel.Children.IndexOf(Current);
        MyWrapPanel.Children.Remove(Current);
        Border NewBorder = new Border();
        NewBorder.Child = Current;
        NewBorder.BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue);
         NewBorder.BorderThickness = new Thickness(1);
         MyWrapPanel.Children.Insert(MyIndex, NewBorder);
        }
      }
    }
    


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by venu238 Monday, October 6, 2014 10:55 AM
    Wednesday, October 1, 2014 2:48 PM
  • Thank you so much Matt Small. This is what I want exactly .


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Monday, October 6, 2014 10:57 AM