Hallo zusammen,
als Neuling in WPF habe ich leider schon ein Problem mit einem vergleichsweise simplen Thema.
Ich würde gerne per Drag/Drop zu verarbeitende Eingabedateien in ein DataGrid meiner Form laden.
Dazu habe ich mir die Drag/Drop-Library von GongSolutions heruntergeladen in der Hoffnung eine Lösung zu finden mit der
ich möglichst wenig selbst implementieren muss.
Leider wird mir das Drag/Drop (Obwohl ich AllowDrop auf True gesetzt habe) auf mein Control aber garnicht erst erlaubt. (Der Cursor zeigt das Sperrsymbol an wenn ich Dateien über das Zielcontrol ziehe)
Diverse Links von google haben mir auch nicht weitergeholfen.
Hat vielleicht jemand änliche Erfahrungen gemacht? Bzw. hat jemand einen Tipp parat wie ich das Problem beheben kann?
Hier mein Code: (In den Zeilen 75-81 seht ihr wo eigentlich das Drag/Drop möglich gemacht werden soll)
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: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122:
| <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Project.Views" xmlns:localVMs="clr-namespace:Project.ViewModels" xmlns:localModels="clr-namespace:Project.Models" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns:md="https://github.com/fantasticfiasco/mvvm-dialogs" xmlns:Wpf="clr-namespace:CSharpControls.Wpf;assembly=CSharpControls.Wpf" x:Class="Project.Views.MainWindow" xmlns:dragDrop="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop" md:DialogServiceViews.IsRegistered="True" mc:Ignorable="d" Title="{Binding Title}" Height="470.608" Width="1072.782" WindowState="Normal">
<Window.Resources> <DataTemplate x:Key="SampleTemplate"> <StackPanel/> </DataTemplate> </Window.Resources> <Window.InputBindings> <KeyBinding Command="{Binding SaveCmd}" Key="S" Modifiers="Ctrl" /> <KeyBinding Command="{Binding SaveAsCmd}" Gesture="Ctrl+Shift+S" /> <KeyBinding Command="{Binding OpenCmd}" Gesture="Ctrl+O" /> <KeyBinding Command="{Binding NewCmd}" Gesture="Ctrl+N" /> <KeyBinding Command="{Binding ExitCmd}" Gesture="Alt+F4" /> </Window.InputBindings>
<DockPanel> <Menu DockPanel.Dock="Top" Margin="0,0,0.4,0"> <MenuItem Header="File"> <MenuItem Header="New" Command="{Binding NewCmd}" InputGestureText="Ctrl+N" /> <MenuItem Header="Open" Command="{Binding OpenCmd}" InputGestureText="Ctrl+O" /> <MenuItem Header="Save" Command="{Binding SaveCmd}" InputGestureText="Ctrl+S" /> <MenuItem Header="Save As" Command="{Binding SaveAsCmd}" InputGestureText="Ctrl+Shift+S"/> <Separator /> <MenuItem Header="Exit" Command="{Binding ExitCmd}" InputGestureText="Alt+F4"/> </MenuItem> <MenuItem Header="About" Command="{Binding ShowAboutDialogCmd}"/> </Menu>
<StackPanel> <Grid Height="152"> <Border CornerRadius="10" BorderThickness="5" Background="Gray" BorderBrush="Gray" Margin="17,10,351,107" IsEnabled="True" Visibility="Hidden"> <Button Content="Land hinzufügen" Background="Gray" BorderBrush="Gray" HorizontalAlignment="Left" Width="139" Command="{Binding AddCountryButtonClickedCmd}" IsEnabled="True"/> </Border>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding CountryCollectionView}" x:Name="datagridview1" CanUserAddRows="False" Background="Silver" Margin="10,55,45,-245" >
<DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Expander IsExpanded="True" ExpandDirection="Right"> <Expander.Header> <StackPanel Orientation="Horizontal"> <TextBlock Text="Eingabedateien" /> </StackPanel> </Expander.Header>
<DataGrid MinHeight="50" AllowDrop="True" dragDrop:DragDrop.IsDropTarget="True" dragDrop:DragDrop.IsDragSource="True" ItemsSource="{Binding CountrySettings.Files}" dragDrop:DragDrop.DragHandler="{Binding DragOver}" dragDrop:DragDrop.DropHandler="{Binding Drop}" > </DataGrid> </Expander> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns>
<DataGrid.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Name}" /> </StackPanel> </DataTemplate> </GroupStyle.HeaderTemplate> <GroupStyle.ContainerStyle> <Style TargetType="{x:Type GroupItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GroupItem}"> <Expander> <Expander.Header> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}" /> </StackPanel> </Expander.Header> <ItemsPresenter /> </Expander> </ControlTemplate> </Setter.Value> </Setter> </Style> </GroupStyle.ContainerStyle> </GroupStyle> </DataGrid.GroupStyle> </DataGrid> </Grid> </StackPanel> </DockPanel> </Window> |
In dem MainViewModel habe ich folgendes implementiert:
C#-Quelltext
1:
| public class MainViewModel : ViewModelBase, IDropTarget |
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9:
| void IDropTarget.DragOver(IDropInfo dropInfo) { var dragFileList = ((DataObject)dropInfo.Data).GetFileDropList().Cast<string>(); dropInfo.Effects = dragFileList.Any(item => { var extension = Path.GetExtension(item); return extension != null && extension.Equals(".tmp"); }) ? DragDropEffects.Copy : DragDropEffects.None; } |
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9:
| void IDropTarget.Drop(IDropInfo dropInfo) { var dragFileList = ((DataObject)dropInfo.Data).GetFileDropList().Cast<string>(); dropInfo.Effects = dragFileList.Any(item => { var extension = Path.GetExtension(item); return extension != null && extension.Equals(".tmp"); }) ? DragDropEffects.Copy : DragDropEffects.None; } |
Viele Grüße!
Moderiert von Christian S.: C#- durch XML-Tags ersetzt