Всем привет, не получается дропнуть файл в контрол и получить его полное имя с путем.
Когда писал на VB.Net делал так и все работало
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
Public NotInheritable Class DragAndDropBehaviour
Private Sub New()
End Sub
#Region "The dependecy Property"
''' <summary>
''' The Dependency property. To allow for Binding, a dependency
''' property must be used.
''' </summary>
Private Shared ReadOnly PreviewDropCommandProperty As DependencyProperty = DependencyProperty.RegisterAttached("PreviewDropCommand", GetType(ICommand), GetType(DragAndDropBehaviour), New PropertyMetadata(AddressOf PreviewDropCommandPropertyChangedCallBack))
#End Region
#Region "The getter and setter"
Public Shared Sub SetPreviewDropCommand(inUIElement As UIElement, inCommand As ICommand)
inUIElement.SetValue(PreviewDropCommandProperty, inCommand)
End Sub
Private Shared Function GetPreviewDropCommand(inUIElement As UIElement) As ICommand
Return DirectCast(inUIElement.GetValue(PreviewDropCommandProperty), ICommand)
End Function
#End Region
#Region "The PropertyChangedCallBack method"
Private Shared Sub PreviewDropCommandPropertyChangedCallBack(inDependencyObject As DependencyObject, inEventArgs As DependencyPropertyChangedEventArgs)
Dim uiElement As UIElement = TryCast(inDependencyObject, UIElement)
If uiElement Is Nothing Then
Return
End If
AddHandler uiElement.Drop, AddressOf procecc_drag_and_drop
End Sub
Private Shared Sub procecc_drag_and_drop(sender As Object, e As DragEventArgs)
Dim files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length = 1 Then
Dim fl_path As String = files.First
If UCase(Right(fl_path, 3)) = "PDF" Then
Dim d = e.Source.datacontext// получаем доступ к ViewModel
d.pdf_path = fl_path// присваиваем имя дропнутого файла полю из ViewModel
End If
Else
MessageBox.Show("Выбрано несколько файлов")
End If
' Assuming you have one file that you care about, pass it off to whatever
' handling code you have defined.
'GetPreviewDropCommand(sender).Execute(e.Data)
'e.Handled = True
End Sub
#End Region
End Class
в свойствах контрола устанавливаем
1.
<Setter Property="behavior:DragAndDropBehaviour.PreviewDropCommand" Value="{Binding PreviewDropCommand}"/>
behavior- пространство имен, где живет класс DragAndDropBehaviour, PreviewDropCommand команда из ViewModel.
Сейчас пишу тоже самое на С#, только так уже не прокатывает.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
public sealed class DragAndDropBehaviour
{
private DragAndDropBehaviour()
{
}
#region "The dependecy Property"
/// <summary>
/// The Dependency property. To allow for Binding, a dependency
/// property must be used.
/// </summary>
#endregion
private static readonly DependencyProperty PreviewDropCommandProperty = DependencyProperty.RegisterAttached("PreviewDropCommand", typeof(ICommand), typeof(DragAndDropBehaviour), new PropertyMetadata(PreviewDropCommandPropertyChangedCallBack));
#region "The getter and setter"
public static void SetPreviewDropCommand(UIElement inUIElement, ICommand inCommand)
{
inUIElement.SetValue(PreviewDropCommandProperty, inCommand);
}
private static ICommand GetPreviewDropCommand(UIElement inUIElement)
{
return (ICommand)inUIElement.GetValue(PreviewDropCommandProperty);
}
#endregion
#region "The PropertyChangedCallBack method"
private static void PreviewDropCommandPropertyChangedCallBack(DependencyObject inDependencyObject, DependencyPropertyChangedEventArgs inEventArgs)
{
UIElement uiElement = inDependencyObject as UIElement;
if (uiElement == null) {
return;
}
uiElement.Drop += procecc_drag_and_drop;
}
private static void procecc_drag_and_drop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length == 1)
{
string fl_path = files.First();
dynamic d = e.Source.DataContext;//'object' не содержит определения для "DataContext" и не удалось найти метод расширения "DataContext",
//принимающий тип 'object' в качестве первого аргумента (возможно, пропущена директива using или ссылка на сборку)
d.pdf_path = fl_path;
} else
{
MessageBox.Show("Выбрано несколько файлов");
}
}
#endregion
}
Подскажите плиз что поправить.