c# - 将 XmlElement 从一个列表框拖动到另一个空的 Xml 绑定(bind)列表框

标签 c# xml wpf listbox xmldocument

我有两个列表框,它们都绑定(bind)到两个不同的 XML 文件。 目的是将 XmlElements 从一个文件拖到另一个文件(ListBox)中。

当我从一个填充的列表框拖动到另一个填充的列表框时,目标列表框中的代码相当简单。 但是,当目标 ListBox 为空时,很难获取任何 XmlElement,因为 ListBox 不包含任何项目。

由于目标未填充,代码将在以下位置失败:

XmlElement targetXmlElement = (XmlElement)parent.Items.GetItemAt(0);

所以问题是: 如何从 ListBox-target 获取 XmlDataProvider 或 XmlDocument:ListBox Parent = (ListBox)sender;

另一个问题是目标列表框应该包含一个子节点列表,即我们拖动元素的目标。 如何访问父元素?

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);
    }

最佳答案

在 WPF 中,您不是直接绑定(bind)到集合,而是绑定(bind)到 default view对于该集合。

All collections have a default CollectionView. WPF always binds to a view rather than a collection. If you bind directly to a collection, WPF actually binds to the default view for that collection. This default view is shared by all bindings to the collection, which causes all direct bindings to the collection to share the sort, filter, group, and current item characteristics of the one default view.

您可以从绑定(bind)源中提取 XmlDocument,而不是获取筛选后的 View 。

private void targetListBox_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 access to the document from the target XML
    BindingExpression bindingExpression = 
        parent.GetBindingExpression(ListBox.ItemsSourceProperty);
    Binding parentBinding = bindingExpression.ParentBinding;
    XmlDataProvider source = (XmlDataProvider)parentBinding.Source;
    XmlDocument targetXmldocument = source.Document;

    AppendXmlNode(sourceXmlElement, targetXmldocument);
}

一旦您掌握了该文档,修改就变得轻而易举。

private static void AppendXmlNode(XmlElement element, XmlDocument doc)
{
    //Get access to the root node to append child
    XmlNode root = doc.SelectSingleNode("/recipelist/recipe[contains(.,'name')]/foodlist");
    //Detach element from source XmlDocument
    XmlNode clone = doc.ImportNode(element, true);
    root.AppendChild(clone);
}

关于c# - 将 XmlElement 从一个列表框拖动到另一个空的 Xml 绑定(bind)列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42302231/

相关文章:

c# - 无法使用 WPF 在横向上打印我的位图

c# - Action<T> vs 匿名方法问题

c# - 是否可以减少我在此代码中使用的构造函数的数量?多个类的构造函数重载和转发

c# - 使用自定义图 block 源进行转换时 UWP MapControl 呈黑色

c# - SpreadsheetML 到 Open XML (XLSX) 的 Excel 转换

xml - 具有多个可选子元素和公共(public)必需元素的 xsd

没有CTRL或Shift的WPF Datagrid多重选择

c# - 如何从控制台应用程序将文件上传到 ASP.NET MVC

xml - 基于xsd/wsdl生成随机xml/web服务响应

wpf - 用矩形填充面板