private async void Main_Drop(object sender, DragEventArgs e) { e.Handled = true; if (e.Data.GetDataPresent("FileName")) { var files = e.Data.GetData("FileName") as string[]; foreach (var file in files) { var longPath = new StringBuilder(255); GetLongPathName(file, longPath, longPath.Capacity); var longPathString = longPath.ToString(); if (Path.GetExtension(longPathString) == ".txt") { ((MainViewModel)Application.Current.MainWindow.DataContext).ImportTxt.Execute(longPathString); } else { ApplicationCommands.Open.Execute(longPathString, this); } } e.Effects = e.AllowedEffects; return; } if (e.Data.GetDataPresent("FileContents")) { using (var contentStream = e.Data.GetData("FileContents") as MemoryStream) { ((MainViewModel)Application.Current.MainWindow.DataContext).ImportTxt.Execute(contentStream); } e.Effects = e.AllowedEffects; return; } var treeViewItem = FlatDocView.FindAncestor <TreeViewItem>((DependencyObject)e.OriginalSource); if (treeViewItem == null) { e.Effects = DragDropEffects.None; return; } var format = FlatDocView.GetDragFormat(e); InfoOwnerData dragData = null; try { dragData = (InfoOwnerData)e.Data.GetData(typeof(InfoOwnerData)); } catch (SerializationException) { return; } var dataContext = ((IItemViewModel)treeViewItem.DataContext).GetModel(); if (dataContext == null || dataContext.GetType().ToString() == format && Equals(dataContext, dragData.ItemData)) { e.Effects = DragDropEffects.None; return; } var document = ((QDocument)DataContext).Document; e.Effects = DragDropEffects.Move; try { if (format == "siqround") { Round round = null; if (dragData != null) { round = (Round)dragData.GetItem(); } else { var value = e.Data.GetData(DataFormats.Serializable).ToString(); using (var stringReader = new StringReader(value)) using (var reader = XmlReader.Create(stringReader)) { round = new Round(); round.ReadXml(reader); } } if (treeViewItem.DataContext is PackageViewModel) { var package = treeViewItem.DataContext as PackageViewModel; package.Rounds.Add(new RoundViewModel(round)); await dragData.ApplyData(document); } else if (treeViewItem.DataContext is RoundViewModel) { var docRound = treeViewItem.DataContext as RoundViewModel; docRound.OwnerPackage.Rounds.Insert(docRound.OwnerPackage.Rounds.IndexOf(docRound), new RoundViewModel(round)); await dragData.ApplyData(document); } } else if (format == "siqtheme") { Theme theme = null; if (dragData != null) { theme = (Theme)dragData.GetItem(); } else { var value = e.Data.GetData(DataFormats.Serializable).ToString(); using (var stringReader = new StringReader(value)) using (var reader = XmlReader.Create(stringReader)) { theme = new Theme(); theme.ReadXml(reader); } } if (treeViewItem.DataContext is RoundViewModel) { var docRound = treeViewItem.DataContext as RoundViewModel; docRound.Themes.Add(new ThemeViewModel(theme)); await dragData.ApplyData(document); } else if (treeViewItem.DataContext is ThemeViewModel) { var docTheme = treeViewItem.DataContext as ThemeViewModel; docTheme.OwnerRound.Themes.Insert(docTheme.OwnerRound.Themes.IndexOf(docTheme), new ThemeViewModel(theme)); await dragData.ApplyData(document); } } else if (format == "siqquestion") { Question question = null; if (dragData != null) { question = (Question)dragData.GetItem(); } else { var value = e.Data.GetData(DataFormats.Serializable).ToString(); using (var stringReader = new StringReader(value)) using (var reader = XmlReader.Create(stringReader)) { question = new Question(); question.ReadXml(reader); } } if (treeViewItem.DataContext is ThemeViewModel) { var docTheme = treeViewItem.DataContext as ThemeViewModel; if (docTheme.Questions.Any(questionViewModel => questionViewModel.Model == question)) { question = question.Clone(); } docTheme.Questions.Add(new QuestionViewModel(question)); if (AppSettings.Default.ChangePriceOnMove) { FlatDocView.RecountPrices(docTheme); } await dragData.ApplyData(document); } else if (treeViewItem.DataContext is QuestionViewModel) { var docQuestion = treeViewItem.DataContext as QuestionViewModel; if (docQuestion.OwnerTheme.Questions.Any(questionViewModel => questionViewModel.Model == question)) { question = question.Clone(); } int pos = docQuestion.OwnerTheme.Questions.IndexOf(docQuestion); docQuestion.OwnerTheme.Questions.Insert(pos, new QuestionViewModel(question)); if (AppSettings.Default.ChangePriceOnMove) { FlatDocView.RecountPrices(docQuestion.OwnerTheme); } await dragData.ApplyData(document); } } else { e.Effects = DragDropEffects.None; } } finally { if (dragData != null) { dragData.Dispose(); } } }
private async void Main_Drop(object sender, DragEventArgs e) { e.Handled = true; line.Visibility = Visibility.Hidden; if (e.Data.GetDataPresent("FileName")) { var files = e.Data.GetData("FileName") as string[]; foreach (var file in files) { var longPath = new StringBuilder(255); GetLongPathName(file, longPath, longPath.Capacity); var longPathString = longPath.ToString(); if (Path.GetExtension(longPathString) == ".txt") { ((MainViewModel)Application.Current.MainWindow.DataContext).ImportTxt.Execute(longPathString); } else { ApplicationCommands.Open.Execute(longPathString, this); } } e.Effects = e.AllowedEffects; return; } if (e.Data.GetDataPresent("FileContents")) { using (var contentStream = e.Data.GetData("FileContents") as MemoryStream) { ((MainViewModel)Application.Current.MainWindow.DataContext).ImportTxt.Execute(contentStream); } e.Effects = e.AllowedEffects; return; } var format = GetDragFormat(e); InfoOwnerData dragData = null; try { dragData = (InfoOwnerData)e.Data.GetData(typeof(InfoOwnerData)); } catch (SerializationException) { return; } e.Effects = DragDropEffects.Move; try { if (format == "siqquestion" && _insertionPosition != null) { Question question = null; if (dragData != null) { question = (Question)dragData.GetItem(); } else { var value = e.Data.GetData(DataFormats.Serializable).ToString(); using (var stringReader = new StringReader(value)) using (var reader = XmlReader.Create(stringReader)) { question = new Question(); question.ReadXml(reader); } } var themeViewModel = _insertionPosition.Item1; var index = _insertionPosition.Item2; if (themeViewModel.Questions.Any(questionViewModel => questionViewModel.Model == question)) { question = question.Clone(); } var questionViewModelNew = new QuestionViewModel(question); themeViewModel.Questions.Insert(index, questionViewModelNew); if (AppSettings.Default.ChangePriceOnMove) { RecountPrices(themeViewModel); } var document = (QDocument)DataContext; await dragData.ApplyData(document.Document); document.Navigate.Execute(questionViewModelNew); } else { e.Effects = DragDropEffects.None; } } finally { if (dragData != null) { dragData.Dispose(); } } }