在WarpPanel中的其他控件之间添加控件 [英] add control between other controls in WarpPanel
问题描述
我的程序中有一个WrapPanel,在运行时向其中添加了一些按钮(例如,在此站点中添加有问题的标签的面板).现在我想在按钮之间单击并在单击鼠标的位置添加一个新按钮.但是我不知道如何获得按钮之间的鼠标位置或如何获取鼠标单击之前放置的按钮的子索引!
我应该说我必须使用WrapPanel,我不想使用Canvas或其他容器.
谢谢您的帮助..
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..
推荐答案
首先,您完全不应该使用Canvas
来设计图形元素,通常使用一些互动或动画行为.
使用System.Windows.Controls.WrapPanel
时,插入元素没有问题,因为其Children
属性是具有Insert
方法的System.Windows.Controls.UIElementCollection
类型.请参阅:
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 [ ^ ].
回答后续问题:
您可以使用FrameworkElement.PredictFocus
方法在面板中找到下一个或上一个元素:
http://msdn.microsoft.com/en-us/library/system. windows.frameworkelement.predictfocus.aspx [ ^ ].
通过单击获取元素的标准不清楚.请自己处理;您掌握了每条信息,可以编写类似的内容.
—SA
First of all, you are perfectly right that you should not useCanvas
which is rather designed to place graphical elements, usually with some interactive or animated behavior.
WithSystem.Windows.Controls.WrapPanel
, there is no problem to insert elements, as itsChildren
property is of the typeSystem.Windows.Controls.UIElementCollection
which hasInsert
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[^].
Answering a follow-up question:
You can find next or previous element in a panel usingFrameworkElement.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
谢尔盖提到的是完全正确的,但是由于我对实际解决方案非常感兴趣,因此这里提供了一种解决方案:
首先在您的XAML中放置一个包装面板,如下所示:
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.
这篇关于在WarpPanel中的其他控件之间添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!