问题
How do I set up a telerik WPF RadTreeView to allow dropping files from explorer onto the tree 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?
回答1:
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;
}
}
来源:https://stackoverflow.com/questions/6094488/drag-drop-files-from-explorer-onto-telerik-wpf-treeview