1

WrapPanelのプログラムには、実行時にいくつかのボタンが追加されるものがあります(このサイトで質問のタグを追加するためのパネルのように)。ボタンの間をクリックして、マウスがクリックされた場所に新しいボタンを追加したいのですが、ボタンの間でマウスの位置を取得する方法や、マウスをクリックする前に配置されたボタンの子インデックスを取得する方法がわかりません!

そして、私は使用しなければならないと言うべきWrapPanelです、私は使用したくない、Canvasまたは他のコンテナ。

ご協力いただきありがとうございます..

4

2 に答える 2

0

私の問題はこのコードによって解決されました:

  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(wpContainer.Children.Count==0 || index==wpContainer.Children.Count) //no items where detected so add it to the end of the Children
            wpContainer.Children.Add(newButton);
    }
于 2012-06-25T07:58:47.520 に答える