按照以前的值

时间:2017-08-22 13:27:01

标签: c# wpf xaml datagrid

所以我有一个“程序”列表,我想这样做,当我按下一个按钮时,我的DataGrid中添加了一行新的“程序”,但我有一些问题,当我添加一个新行程序的数量从零开始我希​​望它从最后一个数字继续。

以下是添加行的方式:

private void Add_download_Click(object sender, RoutedEventArgs e)
{
    for (int a = 0; a < 1; a++)
    {
        PL.List_Programs.Add(UpdateList(a));
    }
}

private Programs UpdateList(int a)
{
    Programs P = new Programs();

    //Add a way to get the number of currently downloading programs (e.g.: ProgramID + a)
    int aAsIDNum = PL.ProgramList().IndexOf();

    //Both of these are used to toghter because I cant make a decimal random generator
    double RndSizeGen = new Random().Next(10, 50);
    int RndSizeGen1 = new Random().Next(1, 9);

    //Generates a random number to be used as the progress precent
    int RndProgressGen = new Random().Next(10, 80);

    Programs _programs = new Programs
    {
        ProgramID = a,
        Name = $"Program{a}",
        Size = Convert.ToDouble($"{RndSizeGen}.{RndSizeGen1}"),
        Progress = RndProgressGen
    };

    //Returns the updated list of programs 
    return _programs;
}

以下是“计划:

的列表
public ObservableCollection<Programs> List_Programs {get; set;}

public ObservableCollection<Programs> ProgramList()
{

    List_Programs = new ObservableCollection<Programs>
    {
        new Programs { _programid = 1, _name = "Program0", _size = 5.8, _progress = 100 },
        new Programs { _programid = 2, _name = "Program1", _size = 7.1, _progress = 36 },
        new Programs { _programid = 3, _name = "Program2", _size = 1.4, _progress = 44 },
        new Programs { _programid = 4, _name = "Program3", _size = 6.5, _progress = 100 },
        new Programs { _programid = 5, _name = "Program4", _size = 2.5, _progress = 89 },
        new Programs { _programid = 6, _name = "Program5", _size = 9.6, _progress = 68 },
        new Programs { _programid = 7, _name = "Program6", _size = 5.3, _progress = 90 },
        new Programs { _programid = 8, _name = "Program7", _size = 2.2, _progress = 100 },
        new Programs { _programid = 9, _name = "Program8", _size = 83.7, _progress = 18 }
    };

    return List_Programs;
}

以下是我使用的变量:

public class Programs : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnProperyChanged(string PropertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
    }

    public int _programid;
    public int ProgramID
    {
        get { return _programid; }
        set
        {
            _programid = value;
            OnProperyChanged(nameof(ProgramID));
        }
    }

    public string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnProperyChanged(nameof(Name));
        }
    }

    public double _size;
    public double Size
    {
        get { return _size; }
        set
        {
            _size = value;
            OnProperyChanged(nameof(Size));
        }
    }

    public  double _progress;
    public double Progress
    {
        get { return _progress; }
        set
        {
            _progress = value;
            OnProperyChanged(nameof(Progress));
        }
    }

    public ICollectionView ProgramsView { get; set; }
}

数据网格/ XAML中

<DataGrid x:Name="DG_List" AutoGenerateColumns="False" IsReadOnly="true"
          ColumnWidth="*" Grid.Column="3" Grid.RowSpan="3">
</DataGrid >

图片供参考: Image for Reference

告诉我是否遗漏了什么

2 个答案:

答案 0 :(得分:0)

for (int a = 0; a < 1; a++)
{
    PL.List_Programs.Add(UpdateList(a));
}

这一行表示您始终使用0 ID创建新项目。 (for循环从0开始并执行1次迭代)

如果未从列表中删除项目,则可以将List.Count值作为新id传递:

PL.List_Programs.Add(UpdateList(PL.List_Programs.Count))

如果可以更改,请找到max id并使用下一个数字:

PL.List_Programs.Add(UpdateList(PL.List_Programs.Max(p=>p.ProgramID)+1))

答案 1 :(得分:0)

每次调用Add_download_Click事件处理程序时增加a的值:

private void Add_download_Click(object sender, RoutedEventArgs e)
{
    int a = 10;
    if (PL.List_Programs.Count > 0)
    {
        a = PL.List_Programs[PL.List_Programs.Count - 1].ProgramID + 1;
    }
    PL.List_Programs.Add(UpdateList(a));
}