我需要在(矩形/面积/边界)中找到UIElement
s。如何在WPF矩形中找到UIElements?
主窗口我做了以下内容:
- 我注册鼠标按下作为起始位置。
- 我regsiter鼠标向上的位置。
- 现在我需要在开始 位置和结束位置之间的矩形中找到ll(按钮,文本框等)。
我在msdn中发现HitTest
的方法,但它只是一个点。我认为,在创建的 矩形中遍历所有点都是性能灾难。基于MVVM模式
http://msdn.microsoft.com/en-us/library/ms752097.aspx
我的代码:
private ObservableCollection<UIElementViewModel> wells;
private Point stratPoint; // Mouse down
public ICommand MouseUpRightCommand
{
get
{
if (this.mouseUpRightCommand == null)
{
this.mouseUpRightCommand = new RelayCommands(
param =>
{
if (param is MouseButtonEventArgs)
{
var e = (param as MouseButtonEventArgs);
//Set the end point
endPosition = e.GetPosition(((ItemsControl)e.Source));
// for example, here I want to find all controls(UIElements) in the
// founded rectangle of stratPoint and endPosition.
}
});
}
return this.mouseUpRightCommand;
}
}
任何其他想法或一个更好的方法?
感谢
这听起来很不错,我现在试试看,谢谢。 – 2012-04-27 15:28:39
嗨,真的,谢谢你的回答,它的工作,但问题这种方法伤害MVVM模式:ObservableCollection井中viewModel是问题FrameWorkElement是一个视图。 –
2012-04-27 16:03:58
你说得对。 但你应该看看这里: [link](http://stackoverflow.com/questions/1083224/pushing-read-only-gui-properties-back-into-viewmodel) 而且可能会创建一个FramworkElementViewModel – astreal 2012-04-27 16:46:54