gpt4 book ai didi

wpf - 拖放WPF用户控件

转载 作者:行者123 更新时间:2023-12-03 10:49:09 28 4
gpt4 key购买 nike

我在wpf中创建了一个拖放控件,以在两个列表框之间拖放数据,这一直很吸引人,直到将其移至另一个项目。

所不同的是,它最初是一个wpf窗口,并使用该窗口对象来获取鼠标位置和控件内部的位置。

this.topWindow = Window.GetWindow(this.sourceItemsControl); //Source items control is instance of ItemsControl

bool previousAllowDrop = this.topWindow.AllowDrop;
this.topWindow.AllowDrop = true;

现在我不得不将其更改为用户控件,因为它是一个较大项目的一部分,该项目是Windows窗体项目,并且 View 从主项目链接为智能零件。因此,现在Window对象为null。

我正在为用户控制寻找类似的功能,但找不到它。我错过了什么?我知道应该有一些东西。

附言:我正在使用MVVM体系结构

最佳答案

找到了使用递归查找基本用户控件的方法,这要感谢ekholm的注意。

public static UserControl FindParentControl(DependencyObject child)
{
DependencyObject parent = VisualTreeHelper.GetParent(child);

//CHeck if this is the end of the tree
if (parent == null) return null;

UserControl parentControl = parent as UserControl;
if (parentControl != null)
{
return parentControl;
}
else
{
//use recursion until it reaches a Window
return FindParentControl(parent);
}
}

现在,该基本用户控件可用于查找坐标(引用)以及设置其他属性,例如 AllowDrop, DragEnter, DragOver等。

关于wpf - 拖放WPF用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11886583/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com