Я не могу перетаскивать элементы в списке и изменять их порядок, следующий мой код xaml:
<ListView x:Name="Images" HorizontalAlignment="Stretch" VerticalAlignment="Top" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5" HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Image Source="{Binding ImageData}" HorizontalAlignment="Stretch" VerticalAlignment="Top" Stretch="UniformToFill" Width="45" Height="45"/>
<TextBlock Text="{Binding Title}" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Это мой код UserControl:
public class DragAndDropListBox<Workstations> : ListBox
where Workstations : class
{
private Point _dragStartPoint;
private P FindVisualParent<P>(DependencyObject child)
where P : DependencyObject
{
var parentObject = VisualTreeHelper.GetParent(child);
if (parentObject == null)
return null;
P parent = parentObject as P;
if (parent != null)
return parent;
return FindVisualParent<P>(parentObject);
}
public DragAndDropListBox()
{
this.PreviewMouseMove += ListBox_PreviewMouseMove;
var style = new Style(typeof(ListBoxItem));
style.Setters.Add(new Setter(ListBoxItem.AllowDropProperty, true));
style.Setters.Add(
new EventSetter(
ListBoxItem.PreviewMouseLeftButtonDownEvent,
new MouseButtonEventHandler(ListBoxItem_PreviewMouseLeftButtonDown)));
style.Setters.Add(
new EventSetter(
ListBoxItem.DropEvent,
new DragEventHandler(ListBoxItem_Drop)));
this.ItemContainerStyle = style;
}
private void ListBox_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point point = e.GetPosition(null);
Vector diff = _dragStartPoint - point;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
var lb = sender as ListBox;
var lbi = FindVisualParent<ListBoxItem>(((DependencyObject)e.OriginalSource));
if (lbi != null)
{
DragDrop.DoDragDrop(lbi, lbi.DataContext, DragDropEffects.Move);
}
}
}
private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_dragStartPoint = e.GetPosition(null);
}
private void ListBoxItem_Drop(object sender, DragEventArgs e)
{
if (sender is ListBoxItem)
{
var source = e.Data.GetData(typeof(Workstations)) as Workstations;
var target = ((ListBoxItem)(sender)).DataContext as Workstations;
int sourceIndex = this.Items.IndexOf(source);
int targetIndex = this.Items.IndexOf(target);
Move(source, sourceIndex, targetIndex);
}
}
private void Move(Workstations source, int sourceIndex, int targetIndex)
{
if (sourceIndex < targetIndex)
{
var items = this.DataContext as IList<Workstations>;
if (items != null)
{
items.Insert(targetIndex + 1, source);
items.RemoveAt(sourceIndex);
}
}
else
{
var items = this.DataContext as IList<Workstations>;
if (items != null)
{
int removeIndex = sourceIndex + 1;
if (items.Count + 1 > removeIndex)
{
items.Insert(targetIndex, source);
items.RemoveAt(removeIndex);
}
}
}
}
}
public partial class Limits : UserControl
{
public Limits()
{
InitializeComponent();
On_Load();
}
}
public class ItemDragAndDropListBox : DragAndDropListBox<Workstations> { }
public partial class Window1 : Window
{
private IList<Workstations> _items = new ObservableCollection<Workstations>();
public Window1()
{
InitializeComponent();
On_Load();
listBox.DataContext = _items;
}
private void On_Load()
{
List<Workstations> image = new List<Workstations>();
this.Images.ItemsSource = new Workstations[]
{
new Workstations{Title=reader["IMAGE_NAME"].ToString(),ImageData=LoadImage(AppDomain.CurrentDomain.BaseDirectory + Images\\"+reader["IMAGE_NAME"].ToString()+".png")},
new MovieData{Title="Movie 2", ImageData=LoadImage(AppDomain.CurrentDomain.BaseDirectory +Images\\"+"LSD_1_2W.png")},
new MovieData{Title="Movie 3",ImageData=LoadImage(AppDomain.CurrentDomain.BaseDirectory +"Images\\"+"LSD_1_2W.png")},
new MovieData{Title="Movie 4", ImageData=LoadImage(AppDomain.CurrentDomain.BaseDirectory + "Images\\"+"LSD_1_2W.png")},
new MovieData{Title="Movie 5", ImageData=LoadImage(AppDomain.CurrentDomain.BaseDirectory + "Images\\"+"LSD_1_2W.png")},
new MovieData{Title="Movie 6", ImageData=LoadImage(AppDomain.CurrentDomain.BaseDirectory + "Images\\"+"LSD_1_2W.png")}};
}
}
this.Images.ItemsSource = image;
}
catch
{
}
}
}
private BitmapImage LoadImage(string filename)
{
return new BitmapImage(new Uri(filename));
}
}
public class Workstations
{
public string Title { get; set; }
public BitmapImage ImageData { get; set; }
}
}
Пожалуйста, дайте мне знать как я могу позволить пользователю переупорядочивать изображения в списке и сохранять их. Все, что я хочу, - это перетаскивать и изменять порядок изображений и сохранять их в списке, который я могу использовать. Я пробовал, но мне не удалось переупорядочить