【问题标题】:How to add control in mouse click point in WrapPanel如何在 WrapPanel 的鼠标单击点中添加控件
【发布时间】:2012-06-24 19:07:16
【问题描述】:

我的程序中有一个WrapPanel,它在运行时添加了一些按钮(例如在此站点中添加问题标签的面板)。现在我想在按钮之间单击并在鼠标单击的地方添加一个新按钮。但我不知道如何在按钮之间获取鼠标位置或如何获取在鼠标单击之前放置的按钮的子索引!

我应该说我必须使用WrapPanel,我不想使用Canvas 或其他容器。

感谢您的帮助..

【问题讨论】:

    标签: c# wpf wrappanel


    【解决方案1】:

    WrapPanelMouseClick 事件中使用此代码:

    Button b = new Button();
    b.Location = new Point(MousePosition.X-this.ClientSize.Width, MousePosition.Y-this.ClientSize.Height);
    this.Controls.Add(b);
    

    更新:

    Button b = new Button();
    b.Location = new Point(MousePosition.X - this.ClientSize.Width, MousePosition.Y - this.ClientSize.Height);
    this.WrapPanel1.Controls.Add(b);
    

    更新 2:

    Button mybutton = new Button();
            mybutton.Content = "This is wpf button";
            Point mousePoint = this.PointToScreen(Mouse.GetPosition(this));  
            MainWindow win = new MainWindow();  
            win.Left = mousePoint.X;  
            win.Top = mousePoint.Y;
            mybutton.PointToScreen(new Point(win.Left,win.Top));
            wrapPanel1.Children.Add(mybutton);
    

    【讨论】:

    • 感谢您的回答,但在 wpf 中没有用于 WrapPanel 的 MouseClick 和用于按钮的 location 属性。我认为您的代码只适用于 WinForms。
    • 我以为你在谈论 WinForms。所以看看我的更新
    • 我在 WrapPanel 的 MouseDown 中编写了这段代码,并在第 7 行得到了这个异常:"This Visual is not connected to a PresentationSource"
    • 您知道如何在鼠标单击位置获取下一个或上一个按钮的索引吗?如果我找到索引,我的问题将得到解决。谢谢
    • int index = mybutton.GetCharacterIndexFromPoint(mousepoint, true);
    【解决方案2】:

    这段代码解决了我的问题:

      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);
        }
    

    【讨论】:

      猜你喜欢
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      相关资源
      最近更新 更多