2 drag n drop events on the same user control using MVVM and Gong WPF Drag n Drop library












0














I'm trying to handle 2 different drag events using the gong-wpf-dragdrop library and MVVM.



First, we drag n drop a file onto the window to load the data.
Second, we drag rows to reorder a DataGrid.



I have no problem getting one OR the other to work. However, when I apply both they conflict with each other.

e.g. I drag a json file onto the form and it de-serializes the object into the form. The object has a list represented with a DataGrid. When I drag a row to reorder them, it triggers my load file method and throws an exception because the data type is wrong.



How can I separate the 2 events so that my reorder drag does not call the file drop method? This is easy with traditional code behind but I'm having difficulty doing this with MVVM and the gong library.



Here are some excerpts from the code. (only relevant parts shown for brevity)
Thanks in advance for any insight you can offer. I'm sure it's something silly.



MyUserControl.xaml
<UserControl x:Class="MyEditor.Views.MyEditor"
xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
<!-- This enables the file drop -->
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DropHandler="{Binding}">
<UserControl.DataContext>
<local:MyViewModel/>
</UserControl.DataContext>

<Grid>
<DataGrid x:Name="MyKeyValueList"
<!-- This enables the row reorder -->
dd:DragDrop.DropScrollingMode="VerticalOnly"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True">
...

</DataGrid>
</Grid>

MyViewModel.cs
// How do I get this to only get called for a file drop?
public void DragOver(IDropInfo dropInfo)
{
var dragFileList = ((DataObject)dropInfo.Data).GetFileDropList().Cast<string>(); // Throws System.InvalidCastException as expected when reordering the DataGrid rows.
dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
dropInfo.Effects = DragDropEffects.Copy;
}

// Same issue. How do I get this to only get called for a file drop?
public void Drop(IDropInfo dropInfo)
{
var dragFileList = ((DataObject)dropInfo.Data).GetFileDropList().Cast<string>();
dropInfo.Effects = dragFileList.Any(item =>
{
Settings = Services.FileServices.ReadFileToSetting(item);
return true;
}) ? DragDropEffects.Copy : DragDropEffects.None;
}









share|improve this question






















  • Instead of casting directly you can first check the data type is correct and then do the cast. If data type is not what you expect, just return from the method without handling the event and DataGrid should handle it afterwards.
    – Yusuf Tarık Günaydın
    Nov 23 at 7:43










  • Thanks for the suggestion Yusuf. I did consider that but it seems there should be a way to call the correct method in the first place.
    – Gary Elliott
    Nov 23 at 21:28
















0














I'm trying to handle 2 different drag events using the gong-wpf-dragdrop library and MVVM.



First, we drag n drop a file onto the window to load the data.
Second, we drag rows to reorder a DataGrid.



I have no problem getting one OR the other to work. However, when I apply both they conflict with each other.

e.g. I drag a json file onto the form and it de-serializes the object into the form. The object has a list represented with a DataGrid. When I drag a row to reorder them, it triggers my load file method and throws an exception because the data type is wrong.



How can I separate the 2 events so that my reorder drag does not call the file drop method? This is easy with traditional code behind but I'm having difficulty doing this with MVVM and the gong library.



Here are some excerpts from the code. (only relevant parts shown for brevity)
Thanks in advance for any insight you can offer. I'm sure it's something silly.



MyUserControl.xaml
<UserControl x:Class="MyEditor.Views.MyEditor"
xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
<!-- This enables the file drop -->
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DropHandler="{Binding}">
<UserControl.DataContext>
<local:MyViewModel/>
</UserControl.DataContext>

<Grid>
<DataGrid x:Name="MyKeyValueList"
<!-- This enables the row reorder -->
dd:DragDrop.DropScrollingMode="VerticalOnly"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True">
...

</DataGrid>
</Grid>

MyViewModel.cs
// How do I get this to only get called for a file drop?
public void DragOver(IDropInfo dropInfo)
{
var dragFileList = ((DataObject)dropInfo.Data).GetFileDropList().Cast<string>(); // Throws System.InvalidCastException as expected when reordering the DataGrid rows.
dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
dropInfo.Effects = DragDropEffects.Copy;
}

// Same issue. How do I get this to only get called for a file drop?
public void Drop(IDropInfo dropInfo)
{
var dragFileList = ((DataObject)dropInfo.Data).GetFileDropList().Cast<string>();
dropInfo.Effects = dragFileList.Any(item =>
{
Settings = Services.FileServices.ReadFileToSetting(item);
return true;
}) ? DragDropEffects.Copy : DragDropEffects.None;
}









share|improve this question






















  • Instead of casting directly you can first check the data type is correct and then do the cast. If data type is not what you expect, just return from the method without handling the event and DataGrid should handle it afterwards.
    – Yusuf Tarık Günaydın
    Nov 23 at 7:43










  • Thanks for the suggestion Yusuf. I did consider that but it seems there should be a way to call the correct method in the first place.
    – Gary Elliott
    Nov 23 at 21:28














0












0








0







I'm trying to handle 2 different drag events using the gong-wpf-dragdrop library and MVVM.



First, we drag n drop a file onto the window to load the data.
Second, we drag rows to reorder a DataGrid.



I have no problem getting one OR the other to work. However, when I apply both they conflict with each other.

e.g. I drag a json file onto the form and it de-serializes the object into the form. The object has a list represented with a DataGrid. When I drag a row to reorder them, it triggers my load file method and throws an exception because the data type is wrong.



How can I separate the 2 events so that my reorder drag does not call the file drop method? This is easy with traditional code behind but I'm having difficulty doing this with MVVM and the gong library.



Here are some excerpts from the code. (only relevant parts shown for brevity)
Thanks in advance for any insight you can offer. I'm sure it's something silly.



MyUserControl.xaml
<UserControl x:Class="MyEditor.Views.MyEditor"
xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
<!-- This enables the file drop -->
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DropHandler="{Binding}">
<UserControl.DataContext>
<local:MyViewModel/>
</UserControl.DataContext>

<Grid>
<DataGrid x:Name="MyKeyValueList"
<!-- This enables the row reorder -->
dd:DragDrop.DropScrollingMode="VerticalOnly"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True">
...

</DataGrid>
</Grid>

MyViewModel.cs
// How do I get this to only get called for a file drop?
public void DragOver(IDropInfo dropInfo)
{
var dragFileList = ((DataObject)dropInfo.Data).GetFileDropList().Cast<string>(); // Throws System.InvalidCastException as expected when reordering the DataGrid rows.
dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
dropInfo.Effects = DragDropEffects.Copy;
}

// Same issue. How do I get this to only get called for a file drop?
public void Drop(IDropInfo dropInfo)
{
var dragFileList = ((DataObject)dropInfo.Data).GetFileDropList().Cast<string>();
dropInfo.Effects = dragFileList.Any(item =>
{
Settings = Services.FileServices.ReadFileToSetting(item);
return true;
}) ? DragDropEffects.Copy : DragDropEffects.None;
}









share|improve this question













I'm trying to handle 2 different drag events using the gong-wpf-dragdrop library and MVVM.



First, we drag n drop a file onto the window to load the data.
Second, we drag rows to reorder a DataGrid.



I have no problem getting one OR the other to work. However, when I apply both they conflict with each other.

e.g. I drag a json file onto the form and it de-serializes the object into the form. The object has a list represented with a DataGrid. When I drag a row to reorder them, it triggers my load file method and throws an exception because the data type is wrong.



How can I separate the 2 events so that my reorder drag does not call the file drop method? This is easy with traditional code behind but I'm having difficulty doing this with MVVM and the gong library.



Here are some excerpts from the code. (only relevant parts shown for brevity)
Thanks in advance for any insight you can offer. I'm sure it's something silly.



MyUserControl.xaml
<UserControl x:Class="MyEditor.Views.MyEditor"
xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
<!-- This enables the file drop -->
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DropHandler="{Binding}">
<UserControl.DataContext>
<local:MyViewModel/>
</UserControl.DataContext>

<Grid>
<DataGrid x:Name="MyKeyValueList"
<!-- This enables the row reorder -->
dd:DragDrop.DropScrollingMode="VerticalOnly"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True">
...

</DataGrid>
</Grid>

MyViewModel.cs
// How do I get this to only get called for a file drop?
public void DragOver(IDropInfo dropInfo)
{
var dragFileList = ((DataObject)dropInfo.Data).GetFileDropList().Cast<string>(); // Throws System.InvalidCastException as expected when reordering the DataGrid rows.
dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
dropInfo.Effects = DragDropEffects.Copy;
}

// Same issue. How do I get this to only get called for a file drop?
public void Drop(IDropInfo dropInfo)
{
var dragFileList = ((DataObject)dropInfo.Data).GetFileDropList().Cast<string>();
dropInfo.Effects = dragFileList.Any(item =>
{
Settings = Services.FileServices.ReadFileToSetting(item);
return true;
}) ? DragDropEffects.Copy : DragDropEffects.None;
}






c# wpf mvvm drag-and-drop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 at 3:25









Gary Elliott

11




11












  • Instead of casting directly you can first check the data type is correct and then do the cast. If data type is not what you expect, just return from the method without handling the event and DataGrid should handle it afterwards.
    – Yusuf Tarık Günaydın
    Nov 23 at 7:43










  • Thanks for the suggestion Yusuf. I did consider that but it seems there should be a way to call the correct method in the first place.
    – Gary Elliott
    Nov 23 at 21:28


















  • Instead of casting directly you can first check the data type is correct and then do the cast. If data type is not what you expect, just return from the method without handling the event and DataGrid should handle it afterwards.
    – Yusuf Tarık Günaydın
    Nov 23 at 7:43










  • Thanks for the suggestion Yusuf. I did consider that but it seems there should be a way to call the correct method in the first place.
    – Gary Elliott
    Nov 23 at 21:28
















Instead of casting directly you can first check the data type is correct and then do the cast. If data type is not what you expect, just return from the method without handling the event and DataGrid should handle it afterwards.
– Yusuf Tarık Günaydın
Nov 23 at 7:43




Instead of casting directly you can first check the data type is correct and then do the cast. If data type is not what you expect, just return from the method without handling the event and DataGrid should handle it afterwards.
– Yusuf Tarık Günaydın
Nov 23 at 7:43












Thanks for the suggestion Yusuf. I did consider that but it seems there should be a way to call the correct method in the first place.
– Gary Elliott
Nov 23 at 21:28




Thanks for the suggestion Yusuf. I did consider that but it seems there should be a way to call the correct method in the first place.
– Gary Elliott
Nov 23 at 21:28

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440307%2f2-drag-n-drop-events-on-the-same-user-control-using-mvvm-and-gong-wpf-drag-n-dro%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440307%2f2-drag-n-drop-events-on-the-same-user-control-using-mvvm-and-gong-wpf-drag-n-dro%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Futebolista

Jornalista