1

我想将树视图中存在的 xmldata 拖放到画布上,但无法得到它。我尝试了一些代码,可能会遗漏一些东西。下面是我的代码。

//handler for mouse move event
private void treeview_mousemove(object sender,MouseEventArgs e) 
{
    if (e.LeftButton == MouseButtonState.Pressed) 
    {
        DataObject data = new DataObject();
        data.SetData(DataFormats.StringFormat, treeView1.SelectedItem.ToString());
        data.SetData("string", treeView1.SelectedItem);
        data.SetData("Object", this);
        DragDrop.DoDragDrop(this,data, DragDropEffects.Copy);
    }
}

//handler for drop event attached in canvas (Don`t know what to do here)  getting 
InvalidOperationException {Logical tree depth exceeded while traversing the tree. This could indicate a cycle in the tree.}
private void treeview_drop(object sender,DragEventArgs e)
{
    e.Effects = DragDropEffects.None;
    UIElement uieve = (UIElement)e.Data.GetData("Object");
    if (e.Data.GetDataPresent(DataFormats.StringFormat))
    {
        canvas1.Children.Add(uieve);
        e.Effects = DragDropEffects.Copy;
    }
}

//handler for drag enter event in canvas
private void treeview_dragenter(object sender, DragEventArgs e)
{
    e.Effects = DragDropEffects.Copy;
}

//handler for dragover event
private void treview_dragover(object sender,DragEventArgs e)
{
    e.Effects = DragDropEffects.None;
    if (e.Data.GetDataPresent(DataFormats.StringFormat))
    {
        string data = (string)e.Data.GetData(DataFormats.StringFormat);
        e.Effects = DragDropEffects.Copy;
    }
}

我想要的只是在画布中放置一个 xml 元素,但不知道该怎么做。提前致谢..

4

1 回答 1

0

我认为您可以通过项目的列表视图替换画布,定义 ItemTemplate 和 DataTemplate。我实际上在我的项目中使用它。拖放功能更简单。

<ListView>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
             <!-- Your item representation -->
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

迈克尔,

于 2013-01-30T08:19:48.500 回答