上下移动的名单

问题描述 投票:0回答:2

我正在使用观察集合来存储我绑定到WPF Listview的列表,我的问题是我正在尝试使前端订单正常工作。在最小值如果我单击我的向上移动按钮,它将项目向上移动,但它不会更改列表中已填充的项目。这应该很容易但我发现很难。

我的问题是如何正确地改变订单号位置以反映用户点击鼠标按钮后基于以下序列并更新列表中的其他项目以反映新位置,因为目前它没有改变数字对现有元素或新的附加元素的correclty。

  Order     Display Name     Width
  1         Title            50
  2         Description      150 

我使用添加按钮将一个项目添加到列表中

  Order     Display Name     Width
  1         Title            50
  2         Description      150 
  3         Newitem          50

一旦新的列表顺序变为,我单击上移按钮

  Order     Display Name     Width
  1         Title            50
  2         Newitem          50 
  3         Description      150

我使用的代码如下

    ObservableCollection<CustomColumnsModel> columnsList = this.WizardData.ConcreteCustomColumnsProxy;
    Extensions.MoveItemUp(columnsList, this.listView1.SelectedIndex);

    int offset = 0;
    var selectedColumnItem = listView1.SelectedItem as CustomColumnsModel;

    foreach (CustomColumnsModel item in columnsList)
    {

        foreach (CustomColumnsModel item in this.listView1.SelectedItems)
        {
            item.CustomColumnsOrder -= 1;
        }else if (listView1.SelectedItem > item)
        {
                item.CustomColumnsOrder +1;
        }



    }

以下是我的扩展方法

  public static void MoveItemUp<T>(this ObservableCollection<T> baseCollection, int selectedIndex)
    {
        //# Check if move is possible
        if (selectedIndex <= 0)
            return;

        //# Move-Item
        baseCollection.Move(selectedIndex - 1, selectedIndex);
    }

这是我的poco课程

 public class CustomColumnsModel : INotifyPropertyChanged
 {
    public event PropertyChangedEventHandler PropertyChanged;
    public const string IdPropertyName = "CustomColumnsID";
    private Guid _Id = Guid.Empty;
    public Guid CustomColumnsID
    {
        get { return _Id; }
        set
        {
            if (_Id == value)
                return;
            _Id = value;
            NotifyPropertyChanged(IdPropertyName);
        }
    }


    public string CustomColumnsDisplayName { get; set; }
    public int CustomColumnsWidth { get; set; }
    public int CustomColumnsOrder { get; set; }  


    protected void NotifyPropertyChanged(string key)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(key));
        }
    }

    public EnterpriseManagementObject ActualData { get; private set; }

编辑以显示基于建议尝试的代码

  foreach (CustomColumnsModel item in columnsList)
    {

         if (item.CustomColumnsOrder < item.ColumnIndex)
         item.CustomColumnsOrder -= 1;
         else if (item.CustomColumnsOrder > item.ColumnIndex)
         item.CustomColumnsOrder -= 1;
   }
c# generic-list
2个回答
0
投票

看起来你需要在你的对象上有一个索引属性,每当有人点击up时,如果它不是最高的那个,你就必须将索引号与它上面的索引号交换。

当有人击中down时,如果它不是最低的,那么再次交换项目。

订单id val 1。 。 。 1。一 2。 。 。 2。二 3。 。 。 3。三

将成为(假设你遇到3):

订单id val 1。 。 。 1。一 3。 。 。 2。二 2。 。 。 3。三

然后按顺序对它们进行排序。


编辑:

    // The following will never be smaller than 0
    if (selectedIndex <= 0)
        return;

    // This is not really doing what you want. 
    baseCollection.Move(selectedIndex - 1, selectedIndex);

你想要一个var item_to_move_up,它等于你所选索引中的项目,以及一个item_to_move_down就是它上面的那个(假设你在逻辑中检查了它不是第一个)。 接下来,交换他们的订单值。就像是:

var temp_val = item_to_move_up.order;
item_to_move_up = item_to_move_down.order;
item_to_move_down = temp_val;

0
投票

我知道这是一篇较老的帖子。但是,如果有人在这里结束......

这是我做的一个例子。

ui - example sorting a collection

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private List<Student> students = new List<Student>()
    {
        new Student(){ name="Jimmy" },
        new Student(){ name="Billy" },
        new Student(){ name="Sarah" },
        new Student(){ name="Bobby" },
        new Student(){ name="Garry" },
        new Student(){ name="Eva" },
        new Student(){ name="Nancy" }
    };
    private Student _student;

    private class Student
    {
        public string name { get; set; }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MoveUpDn(true);
    }
    private void MoveUpDn(bool blnUp)
    {
        if (_student == null) return;
        int idx = students.IndexOf(_student);
        if (blnUp)
        {
            if (idx <= 0) return;
            students.Insert(idx - 1, _student);
            students.RemoveAt(idx + 1);
            idx--;
        }
        else
        {
            if (idx >= students.Count - 1) return;
            students.Insert(idx + 2, _student);
            students.RemoveAt(idx);
            idx++;
        }
        UpdateMyList();
        this.listBox1.SelectedIndex = idx;
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        UpdateMyList();
    }
    private void UpdateMyList()
    {
        this.listBox1.DataSource = null;
        this.listBox1.DataSource = students;
        this.listBox1.DisplayMember = "name";
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem == null) return;
        if (listBox1.SelectedItem.GetType() != typeof(Student)) return;
        Student student = this.listBox1.SelectedItem as Student;
        if (student == null) return;
        _student = student;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        MoveUpDn(false);
    }
}
}
© www.soinside.com 2019 - 2024. All rights reserved.