<Button.Template> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Button.Template> <Button.Template> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Button.Template>
NuGet添加库引用： 确保项目中已经添加了 System.Windows.Interactivity 和 MvvmLightLibsxmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:command="http://www.galasoft.ch/mvvmlight" AllowDrop="True" <i:Interaction.Triggers> <i:EventTrigger EventName="DragEnter"> <command:EventToCommand Command="{Binding DragEnterAndOverCommand}" PassEventArgsToCommand="True"/> </i:EventTrigger> <i:EventTrigger EventName="DragOver"> <command:EventToCommand Command="{Binding DragEnterAndOverCommand}" PassEventArgsToCommand="True"/> </i:EventTrigger> <i:EventTrigger EventName="Drop"> <command:EventToCommand Command="{Binding DropCommand}" PassEventArgsToCommand="True"/> </i:EventTrigger> <i:EventTrigger EventName="DragLeave"> <command:EventToCommand Command="{Binding DragLeaveCommand}" PassEventArgsToCommand="True"/> </i:EventTrigger> </i:Interaction.Triggers> <Border Grid.ColumnSpan="2" Background="#80808080" Visibility="{Binding IsImport}"/> // 是否正在拖放图片 private string _isImport = "Collapsed"; public string IsImport { get => _isImport; set => Set(ref _isImport, value, nameof(IsImport)); } public RelayCommand<DragEventArgs> DragEnterAndOverCommand { get; private set; } public RelayCommand<DragEventArgs> DropCommand { get; private set; } public RelayCommand<DragEventArgs> DragLeaveCommand { get; private set; } // 拖拽进入、移动 DragEnterAndOverCommand = new RelayCommand<DragEventArgs>(DragEnterAndOver); // 拖拽放入 DropCommand = new RelayCommand<DragEventArgs>(Drop); // 拖拽离开 DragLeaveCommand = new RelayCommand<DragEventArgs>(DragLeave); // 拖拽进入、移动 private void DragEnterAndOver(DragEventArgs e) { // 检查拖入的数据是否为文件类型 if (e.Data.GetDataPresent(DataFormats.FileDrop)) { IsImport = "Visible"; e.Effects = DragDropEffects.Copy; // 显示复制的鼠标样式 } else { IsImport = "Collapsed"; e.Effects = DragDropEffects.None; // 显示禁止的鼠标样式 } e.Handled = true; } // 拖拽放入 private void Drop(DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { try { // 获取拖放的所有文件路径 string[] selectFiles = (string[])e.Data.GetData(DataFormats.FileDrop); // 定义支持的文件扩展名列表 string[] extensions = new string[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp", ".ico" }; List<string> imageFiles = new List<string>(); HashSet<string> existingFiles = new HashSet<string>(ImagePaths); // 遍历所有拖放的文件和文件夹 foreach (var path in selectFiles) { if (Directory.Exists(path)) { // 如果是文件夹，递归获取所有图片文件 Stack<string> dirs = new Stack<string>(new string[] { path }); while (dirs.Count > 0) { string currentDir = dirs.Pop(); try { foreach (var file in Directory.GetFiles(currentDir)) { if (extensions.Contains(Path.GetExtension(file).ToLower()) && !existingFiles.Contains(file)) { imageFiles.Add(file); existingFiles.Add(file); } } foreach (var subDir in Directory.GetDirectories(currentDir)) { dirs.Push(subDir); } } catch (Exception ex) { // 处理文件夹读取异常 HandyControl.Controls.MessageBox.Show($"读取文件夹时出现错误：{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } } else if (File.Exists(path)) { // 如果是文件，检查扩展名并添加到图片列表中 if (extensions.Contains(Path.GetExtension(path).ToLower()) && !existingFiles.Contains(path)) { imageFiles.Add(path); existingFiles.Add(path); } } } if (SequentialTypesetting) { // 添加符合扩展名的文件到 ImagePaths 列表中 foreach (var file in imageFiles) { ImagePaths.Add(file); } } else if (RandomTypesetting) { // 创建Random实例 var random = new Random(); // 打乱文件列表 var shuffledFiles = imageFiles.OrderBy(x => random.Next()).ToList(); // 将列表分割为两部分 int middle = shuffledFiles.Count / 2; var firstHalf = shuffledFiles.Take(middle).ToList(); var secondHalf = shuffledFiles.Skip(middle).ToList(); // 交错合并两个列表 var mixedFiles = new List<string>(); int maxLength = Math.Max(firstHalf.Count, secondHalf.Count); for (int i = 0; i < maxLength; i++) { if (i < secondHalf.Count) mixedFiles.Add(secondHalf[i]); if (i < firstHalf.Count) mixedFiles.Add(firstHalf[i]); } // 保存文件路径 foreach (var file in mixedFiles) { ImagePaths.Add(file); } } // 标记事件已处理 e.Handled = true; // 导入图片数量 ImportedImagesCount = ImagePaths.Count; if (StitchableImagesCount == 0 || StitchAfterNImages == 0) { // 拼接图片数量 StitchableImagesCount = 1; } else { // 可拼接图片数量 StitchableImagesCount = (int)Math.Ceiling((double)ImportedImagesCount / StitchAfterNImages); } // 显示返回按钮 BackVisible = "Visible"; // 检查是否有图片文件 if (ImagePaths.Any()) { IsEnableParameter = true; IsImageWatermarkSelected = false; IsTextWatermarkSelected = false; WatermarkImagePath = ""; WatermarkImageWidth = 0; WatermarkImageHeight = 0; WatermarkText = ""; FontType = "微软雅黑"; FontSize = 30; TextColor = "#FFFFFF"; WatermarkRotationAngle = 0; WatermarkOpacity = 100; WatermarkPosition = "右下"; WatermarkSpacing = 200; Preview(); } else { IsEnableParameter = false; HandyControl.Controls.MessageBox.Show("所选文件夹中没有找到任何图片文件！", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); SelectedFolderPath = string.Empty; } } catch (Exception ex) { IsEnableParameter = false; HandyControl.Controls.MessageBox.Show($"加载图片路径时出现错误：{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); // 标记事件已处理 e.Handled = true; } finally { IsImport = "Collapsed"; } } } // 拖拽离开 private void DragLeave(DragEventArgs e) { IsImport = "Collapsed"; e.Handled = true; }
<TextBox Text="{Binding SaveHeight, UpdateSourceTrigger=PropertyChanged}" Width="70" Grid.Column="1" Margin="5 0 0 0" Style="{StaticResource TextBoxExtend}" />