示例#1
0
        protected override void OnDrop(DragEventArgs dea)
        {
            if (dea.Data.GetDataPresent(DataFormats.FileDrop))
            {
                var files = dea.Data.GetData(DataFormats.FileDrop) as string[];
                if (files == null)
                {
                    return;
                }

                if (Images.HasImageExtension(files[0]))
                {
                    var dialog = new ImageDropDialog
                    {
                        Owner            = Application.Current.MainWindow,
                        TextEditor       = EditBox,
                        DocumentFileName = FileName,
                        DragEventArgs    = dea
                    };
                    dialog.ShowDialog();
                }
                else
                {
                    Dispatcher.InvokeAsync(() => OpenFileCommand.Command.Execute(files[0], this));
                }
            }
        }
示例#2
0
        protected override void OnDrop(DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                var files = e.Data.GetData(DataFormats.FileDrop) as string[];
                if (files == null)
                {
                    return;
                }
                var imageExtensions = new[] { ".jpg", "jpeg", ".png", ".gif" };

                if (imageExtensions.Any(ext => files[0].EndsWith(ext, StringComparison.OrdinalIgnoreCase)))
                {
                    var dialog = new ImageDropDialog
                    {
                        Owner         = Application.Current.MainWindow,
                        TextEditor    = EditBox,
                        DragEventArgs = e
                    };
                    dialog.ShowDialog();
                }
                else
                {
                    Dispatcher.InvokeAsync(() => OpenFile(files[0]));
                }
            }
        }
示例#3
0
        private void AllowImagePaste()
        {
            // AvalonEdit only allows text paste. Hack the command to allow otherwise.
            var cmd = EditBox.TextArea.DefaultInputHandler.Editing.CommandBindings.FirstOrDefault(cb => cb.Command == Paste);

            if (cmd == null)
            {
                return;
            }

            CanExecuteRoutedEventHandler canExecute = (sender, args) =>
                                                      args.CanExecute = EditBox.TextArea?.Document != null &&
                                                                        EditBox.TextArea.ReadOnlySectionProvider.CanInsert(EditBox.TextArea.Caret.Offset);

            ExecutedRoutedEventHandler execute = null;

            execute = (sender, args) =>
            {
                if (System.Windows.Clipboard.ContainsText())
                {
                    // WPF won't continue routing the command if there's PreviewExecuted handler.
                    // So, remove it, call Execute and reinstall the handler.
                    // Hack, hack hack...
                    try
                    {
                        cmd.PreviewExecuted -= execute;
                        cmd.Command.Execute(args.Parameter);
                    }
                    finally
                    {
                        cmd.PreviewExecuted += execute;
                    }
                }
                else if (System.Windows.Clipboard.ContainsImage())
                {
                    var dialog = new ImageDropDialog
                    {
                        Owner             = Application.Current.MainWindow,
                        TextEditor        = EditBox,
                        DocumentFileName  = FileName,
                        UseClipboardImage = true
                    };
                    dialog.ShowDialog();
                    args.Handled = true;
                }
            };

            cmd.CanExecute      += canExecute;
            cmd.PreviewExecuted += execute;
        }