开发者

Drag drop files from explorer onto Telerik WPF treeview

How do I set up a telerik WPF RadTreeView to allow dropping files from explorer onto the tre开发者_StackOverflow中文版e nodes?

We go the normal drag n drop events working, but they don't let us know what node we dropped on. We are currently trying to use the Telerik custom drag n drop support but with no luck.

Ideas?


It is pretty simple, you just have to wire it up your self. Here is a simple example.

Then this the XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:core="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation">
<Grid>

    <telerik:RadTreeView x:Name="rtvMain" AllowDrop="True" IsDragDropEnabled="True" ItemPrepared="rtvMain_ItemPrepared">
        <telerik:RadTreeViewItem Header="Sport Categories">
            <telerik:RadTreeViewItem Header="Football">
                <telerik:RadTreeViewItem Header="Futsal" />
                <telerik:RadTreeViewItem Header="Soccer"/>
            </telerik:RadTreeViewItem>
            <telerik:RadTreeViewItem Header="Tennis"/>
            <telerik:RadTreeViewItem Header="Cycling"/>
        </telerik:RadTreeViewItem>
    </telerik:RadTreeView>
</Grid>

Code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void rtvMain_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
    {
        e.PreparedItem.Drop += new DragEventHandler(PreparedItem_Drop);
    }

    private void PreparedItem_Drop(object sender, DragEventArgs e)
    {
        var destinationItem = sender as RadTreeViewItem;

        if (destinationItem != null)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            foreach (string file in files)
            {
                Console.WriteLine(String.Format("Trying to put {0} into the folder {1}...", file, destinationItem));
            }
        }
        e.Handled = true;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜