2つのリストボックスがあり、両方が2つの異なるXMLファイルにバインドされています。 目的は、XmlElementsをあるファイルから別のファイル(ListBox)にドラッグすることです。XmlElementを1つのリストボックスから別の空のXMLバインドリストボックスにドラッグ
入力されたListBoxから他の入力されたListBoxにドラッグすると、ターゲットのListBoxのコードはかなり簡単です。 しかし、ターゲットListBoxが空の場合、ListBoxにはアイテムが含まれていないため、XmlElementsを取得することは困難です。
ターゲットはコードがで失敗します移入されませんので:
XmlElement targetXmlElement = (XmlElement)parent.Items.GetItemAt(0);
そこで質問です:私は、リストボックス、ターゲットからにXmlDataProviderまたはXmlDocumentオブジェクトを手に入れるにはどうすればよい : ListBox parent = (ListBox)sender;
もう1つの問題は、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は何も返しません。 –