文件命名格式设置支持拖拽

pull/409/head
leiurayer 3 years ago
parent cf3221d601
commit b5c5776ac8

@ -10,6 +10,7 @@ using Prism.Regions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq; using System.Linq;
namespace DownKyi.ViewModels.Settings namespace DownKyi.ViewModels.Settings
@ -185,6 +186,20 @@ namespace DownKyi.ViewModels.Settings
// 文件命名格式 // 文件命名格式
SelectedFileName = new ObservableCollection<DisplayFileNamePart>(); SelectedFileName = new ObservableCollection<DisplayFileNamePart>();
SelectedFileName.CollectionChanged += new NotifyCollectionChangedEventHandler((sender, e) =>
{
// 当前显示的命名格式part
List<FileNamePart> fileName = new List<FileNamePart>();
foreach (DisplayFileNamePart item in SelectedFileName)
{
fileName.Add(item.Id);
}
bool isSucceed = SettingsManager.GetInstance().SetFileNameParts(fileName);
PublishTip(isSucceed);
});
OptionalFields = new ObservableCollection<DisplayFileNamePart>(); OptionalFields = new ObservableCollection<DisplayFileNamePart>();
foreach (FileNamePart item in Enum.GetValues(typeof(FileNamePart))) foreach (FileNamePart item in Enum.GetValues(typeof(FileNamePart)))
{ {
@ -511,16 +526,18 @@ namespace DownKyi.ViewModels.Settings
SetVideoContent(); SetVideoContent();
} }
// 选中文件名字段点击事件 // 选中文件名字段右键点击事件
private DelegateCommand<object> selectedFileNameCommand; private DelegateCommand<object> selectedFileNameRightCommand;
public DelegateCommand<object> SelectedFileNameCommand => selectedFileNameCommand ?? (selectedFileNameCommand = new DelegateCommand<object>(ExecuteSelectedFileNameCommand)); public DelegateCommand<object> SelectedFileNameRightCommand => selectedFileNameRightCommand ?? (selectedFileNameRightCommand = new DelegateCommand<object>(ExecuteSelectedFileNameRightCommand));
/// <summary> /// <summary>
/// 选中文件名字段点击事件 /// 选中文件名字段右键点击事件
/// </summary> /// </summary>
/// <param name="parameter"></param> /// <param name="parameter"></param>
private void ExecuteSelectedFileNameCommand(object parameter) private void ExecuteSelectedFileNameRightCommand(object parameter)
{ {
if (parameter == null) { return; }
bool isSucceed = SelectedFileName.Remove((DisplayFileNamePart)parameter); bool isSucceed = SelectedFileName.Remove((DisplayFileNamePart)parameter);
if (!isSucceed) if (!isSucceed)
{ {
@ -528,14 +545,14 @@ namespace DownKyi.ViewModels.Settings
return; return;
} }
List<FileNamePart> fileName = new List<FileNamePart>(); //List<FileNamePart> fileName = new List<FileNamePart>();
foreach (DisplayFileNamePart item in SelectedFileName) //foreach (DisplayFileNamePart item in SelectedFileName)
{ //{
fileName.Add(item.Id); // fileName.Add(item.Id);
} //}
isSucceed = SettingsManager.GetInstance().SetFileNameParts(fileName); //isSucceed = SettingsManager.GetInstance().SetFileNameParts(fileName);
PublishTip(isSucceed); //PublishTip(isSucceed);
SelectedOptionalField = -1; SelectedOptionalField = -1;
} }

@ -264,15 +264,22 @@
Grid.Row="1" Grid.Row="1"
Grid.Column="1" Grid.Column="1"
MinHeight="30" MinHeight="30"
ItemContainerStyle="{StaticResource TagItem2Style}"
ItemsSource="{Binding SelectedFileName, Mode=TwoWay}" ItemsSource="{Binding SelectedFileName, Mode=TwoWay}"
SelectionMode="Single" SelectionMode="Single"
Style="{StaticResource Tag2Style}"> Style="{StaticResource Tag2Style}">
<i:Interaction.Triggers> <i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged"> <i:EventTrigger EventName="MouseRightButtonUp">
<i:InvokeCommandAction Command="{Binding SelectedFileNameCommand}" CommandParameter="{Binding ElementName=nameSelectedFileName, Path=SelectedItem}" /> <i:InvokeCommandAction Command="{Binding SelectedFileNameRightCommand}" CommandParameter="{Binding ElementName=nameSelectedFileName, Path=SelectedItem}" />
</i:EventTrigger> </i:EventTrigger>
</i:Interaction.Triggers> </i:Interaction.Triggers>
<ListBox.ItemContainerStyle>
<Style BasedOn="{StaticResource TagItem2Style}" TargetType="{x:Type ListBoxItem}">
<Setter Property="AllowDrop" Value="True" />
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="SelectedFileName_PreviewMouseLeftButtonDown" />
<EventSetter Event="Drop" Handler="SelectedFileName_Drop" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox> </ListBox>
<StackPanel <StackPanel

@ -1,4 +1,8 @@
using System.Windows.Controls; using DownKyi.ViewModels.Settings;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace DownKyi.Views.Settings namespace DownKyi.Views.Settings
{ {
@ -11,5 +15,44 @@ namespace DownKyi.Views.Settings
{ {
InitializeComponent(); InitializeComponent();
} }
// ListBox拖拽的代码参考
// https://stackoverflow.com/questions/3350187/wpf-c-rearrange-items-in-listbox-via-drag-and-drop
private void SelectedFileName_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender is ListBoxItem)
{
ListBoxItem draggedItem = sender as ListBoxItem;
DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
draggedItem.IsSelected = true;
}
}
private void SelectedFileName_Drop(object sender, DragEventArgs e)
{
DisplayFileNamePart droppedData = e.Data.GetData(typeof(DisplayFileNamePart)) as DisplayFileNamePart;
DisplayFileNamePart target = ((ListBoxItem)sender).DataContext as DisplayFileNamePart;
int removedIdx = nameSelectedFileName.Items.IndexOf(droppedData);
int targetIdx = nameSelectedFileName.Items.IndexOf(target);
var ItemsSource = (ObservableCollection<DisplayFileNamePart>)nameSelectedFileName.ItemsSource;
if (removedIdx < targetIdx)
{
ItemsSource.Insert(targetIdx + 1, droppedData);
ItemsSource.RemoveAt(removedIdx);
}
else
{
int remIdx = removedIdx + 1;
if (ItemsSource.Count + 1 > remIdx)
{
ItemsSource.Insert(targetIdx, droppedData);
ItemsSource.RemoveAt(remIdx);
}
}
}
} }
} }

Loading…
Cancel
Save