둘 다 두 개의 다른 XML 파일에 바인딩되는 두 개의 목록 상자가 있습니다. XmlElements를 한 파일에서 다른 파일 (ListBox)로 드래그하는 것이 목적입니다.하나의 목록 상자에서 다른 빈 Xml 바운드 목록 상자로 XmlElement 끌기
채워진 ListBox에서 채워진 다른 ListBox로 드래그하면 대상 ListBox의 코드가 매우 간단합니다. 그러나 대상 ListBox가 비어있는 경우 ListBox에 항목이 없으므로 XmlElements를 가져 오는 것은 어렵습니다.
대상이 코드에 실패 채워지지 않기 때문에 :
XmlElement targetXmlElement = (XmlElement)parent.Items.GetItemAt(0);
그래서 질문은 다음과 같습니다 어떻게 내가옵니다 대상에서 XmlDataProvider 또는을 XmlDocument의 보류를받을 수 있나요 : ListBox parent = (ListBox)sender;
또 다른 문제점은 target-listbox에 드래그 한 요소의 대상 인 하위 노드 목록이 포함되어야한다는 것입니다. 상위 요소에 대한 액세스 권한을 얻으려면 어떻게해야합니까?
ListBox dragSource = null;
private void FoodListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ListBox parent = (ListBox)sender;
dragSource = parent;
object data = GetDataFromListBox(dragSource, e.GetPosition(parent));
if (data != null)
{
DragDrop.DoDragDrop(parent, data, DragDropEffects.Copy);
}
}
#region GetDataFromListBox(Listbox, Point)
private static object GetDataFromListBox(ListBox source, Point point)
{
UIElement element = source.InputHitTest(point) as UIElement;
if(element != null)
{
object data = DependencyProperty.UnsetValue;
while(data == DependencyProperty.UnsetValue)
{
data = source.ItemContainerGenerator.ItemFromContainer(element);
if (data == DependencyProperty.UnsetValue)
{
element = VisualTreeHelper.GetParent(element) as UIElement;
}
if (element == source)
{
return null;
}
}
if(data != DependencyProperty.UnsetValue)
{
return data;
}
}
return null;
}
#endregion
//This listbox is bound to Dataprovider2, Objects dragged into will access the XML target
private void ListBox_Drop(object sender, DragEventArgs e)
{
ListBox parent = (ListBox)sender;
//Get access to the element from the source XML
XmlElement sourceXmlElement = (XmlElement)e.Data.GetData(typeof(XmlElement));
//Get the position of the parent to any Element in the the target list (e.g the zero element)
XmlElement targetXmlElement = (XmlElement)parent.Items.GetItemAt(0);
AppendXmlNode(sourceXmlElement, targetXmlElement);
}
xpath는 아무것도 반환하지 않습니다. –