Autor Beitrag
haschme
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 57
Erhaltene Danke: 1



BeitragVerfasst: Fr 04.05.18 14:04 
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)
ausblenden volle Höhe XML-Daten
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>
        <!-- Insert here your Data Templates -->
        <DataTemplate x:Key="SampleTemplate">
            <StackPanel/>
        </DataTemplate>
    </Window.Resources>
    <Window.InputBindings>
        <!-- Example of Keyboard shortcuts -->
        <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>
        <!-- File Menu -->
        <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>
            <!-- Place here your content -->
        </StackPanel>
    </DockPanel>
</Window>


In dem MainViewModel habe ich folgendes implementiert:

ausblenden C#-Quelltext
1:
public class MainViewModel : ViewModelBase, IDropTarget					


ausblenden 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;
        }


ausblenden 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 user profile iconChristian S.: C#- durch XML-Tags ersetzt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 04.05.18 15:05 
Startest du die App über Visual Studio, d.h. im Debugging-Mode? Dann lies mal Why my WPF application has Drag & Drop disabled (even when AllowDrop is true)? und WPF Allow Drop.

Für diesen Beitrag haben gedankt: haschme
haschme Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 57
Erhaltene Danke: 1



BeitragVerfasst: Fr 04.05.18 16:31 
user profile iconTh69 hat folgendes geschrieben Zum zitierten Posting springen:
Startest du die App über Visual Studio, d.h. im Debugging-Mode? Dann lies mal Why my WPF application has Drag & Drop disabled (even when AllowDrop is true)? und WPF Allow Drop.


Ja habe VS im Debugging Mode verwendet.
Allerdings ist mir aufgefallen, dass ein DragDrop bei den anderen Controls möglich zu sein scheint.
Ich habe das AllowDrop mal testweise im DataGrid.GroupStyle innerhalb des Itempresenters erlaubt. (Zeile 109 des XAML-Codes)
Dadurch wird ein Drag/Drop innerhalb des DataGridTemplates möglich.
Nur das innenliegende Grid akzeptiert kein DragDrop.

Im Anhang findet Ihr ein Screenshot um zu zeigen wie genau mein Problem aussieht.
In dem Bereich wo der rote Pfeil hinzeigt kann ich Drag/Drop verwenden (seit ich den Itempresenter AllowDrop="True" zugewiesen habe)
da wo der blaue Pfeil hinzeigt, würde ich es gerne können (dort funktioniert es aber nicht trotz AllowDrop="True")

Habe ich vielleicht irgendwas vergessen? :shock:
Einloggen, um Attachments anzusehen!
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 04.05.18 17:55 
Ich denke, du mußt explizit das AllowDrop für die TextBoxColumns setzen, d.h. setze AutoGenerateColumns="False" und definiere selber die DataGrid-Columns und setze dort dann
ausblenden XML-Daten
1:
<TextBlock Text="{Binding FilePath}" AllowDrop="True" />					

Für diesen Beitrag haben gedankt: haschme
haschme Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 57
Erhaltene Danke: 1



BeitragVerfasst: Di 08.05.18 14:05 
Danke, das mit dem Textblock hat mir definitiv weitergeholfen.
Allerdings hat das Aufrufen der jeweiligen Methoden aus dem GongSolutions-Framework nicht funktioniert.
Ich habe mich deshalb schweren Herzens ersteinmal von dem Grundgedanken den View komplett leer zu lassen verabschiedet und dort das
entsprechende Drop-Event selbst implementiert.

Viele Grüße!

Der Thread kann geschlossen werden