Click here to Skip to main content
14,304,866 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a WrapPanel in my program that some button add to it in runtime (like the panel for add tags for question in this site). now I want to click between the buttons and add a new button in the place mouse is clicked.but I don't know how can I get mouse position between button or how to get child index of the button that placed before mouse click!

And I should to say I have to use WrapPanel, I dont want use Canvas or other container.

Thanks for your help..
Posted
Comments
   
Not clear what is the problem. If your MSDN is broken, I hope Google and Bind did not ban you. Or... did they? :-)
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

First of all, you are perfectly right that you should not use Canvas which is rather designed to place graphical elements, usually with some interactive or animated behavior.

With System.Windows.Controls.WrapPanel, there is no problem to insert elements, as its Children property is of the type System.Windows.Controls.UIElementCollection which has Insert method. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.wrappanel.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.children.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection.insert.aspx[^].

[EDIT]

Answering a follow-up question:

You can find next or previous element in a panel using FrameworkElement.PredictFocus method:
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.predictfocus.aspx[^].

Your criteria for getting an element by click is not clear. Please deal with it by yourself; you have every piece of information at your hands to write something like that.

—SA
   
v2
Comments
sheida.a 25-Jun-12 2:10am
   
thanks for your answer.I know how to add child to WarpPanel but I want to insert a child in position that mouse clicked between children.in other words I want to find the next and previous child index in mouse click position.
I hope u get what I mean..
   
Yes, I got it in first place. What seems to be a problem? I though you can find it out from these references.
I wounder why doing such weird things...
--SA
Amir Mahfoozi 25-Jun-12 3:23am
   
+5
   
Thank you, Amir.
--SA
Amir Mahfoozi 25-Jun-12 3:28am
   
You're welcome.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hi,
What Sergey mentioned is completely correct but as I'm very interested in practical solutions here is one solution:

First place a wrap panel in your XAML as here :
<WrapPanel x:Name="wpContainer" VerticalAlignment="Top" Height="100" Width="200" MouseDown="wpContainer_MouseDown" 

                   HorizontalAlignment="Stretch" Background="Yellow"></WrapPanel>


The width and height is for preventing it from collapsing when it has no child.
And place mouse down event handler in related code file:
private void wpContainer_MouseDown(object sender, MouseButtonEventArgs e)
{
    Button newButton = new Button()
                     {
                         Content= wpContainer.Children.Count,

                         Margin = new Thickness()
                         {
                             Right = 10,

                             Left = 10,
                         }
                     };

    var mousePosition = Mouse.GetPosition(wpContainer);

    int index=0;

    foreach (var child in wpContainer.Children)
    {
        Button currentButton = (child as Button);

        if (currentButton==null)
            continue;

        Point buttonPosition = currentButton.TransformToAncestor(wpContainer).Transform(new Point(0, 0));

        if (buttonPosition.X  > mousePosition.X && buttonPosition.Y+currentButton.ActualHeight > mousePosition.Y)
        {
            wpContainer.Children.Insert(index, newButton);

            return;
        }

        index++;
    }

    if(index==wpContainer.Children.Count) //no items where detected so add it to the end of the Children
        wpContainer.Children.Add(newButton);
}


Change it as you wish to fulfill your needs.

Good Luck.
   
Comments
sheida.a 25-Jun-12 4:16am
   
Thanks a milion..
Amir Mahfoozi 25-Jun-12 4:17am
   
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100