Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 插入的ArgumentOutOfRangeExecution_C#_Wpf - Fatal编程技术网

C# 插入的ArgumentOutOfRangeExecution

C# 插入的ArgumentOutOfRangeExecution,c#,wpf,C#,Wpf,嘿,用户 lista.insert(…)的声明有问题 这个项目的主要任务是,我可以通过点击Listview中2列的按钮来添加。。。我得到了基本结构,但不知怎的,他总是给我一个关于范围执行错误的论点。。。我减少了所有其他部分,只包含了主要功能。我希望这能引导某人解决这个愚蠢的问题 XML: 主窗口: namespace WpfApplication5 { /// <summary> /// Interaktionslogik für MainWindow.xaml /// <

嘿,用户

lista.insert(…)的声明有问题

这个项目的主要任务是,我可以通过点击Listview中2列的按钮来添加。。。我得到了基本结构,但不知怎的,他总是给我一个关于范围执行错误的论点。。。我减少了所有其他部分,只包含了主要功能。我希望这能引导某人解决这个愚蠢的问题

XML:


主窗口:

namespace WpfApplication5
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public ObservableCollection<Lista> lista { get; set; }
    private RelayCommand cmd;


    public RelayCommand Cmd
    {
        get { return cmd ?? (cmd = new RelayCommand(new Action<object>(AddRow))); }
    }
    public MainWindow()
    {
        lista = new ObservableCollection<Lista> {};


        InitializeComponent();
        this.DataContext = this;

    }
    public void AddRow(object obj)
    {
        int numer;
        Int32.TryParse(obj.ToString(), out numer);
       lista.Insert(numer, new Lista()
        {  hexnumber_op_Code = "10",OP_Code = "1",});

    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        lista.Insert(10, new Lista() { Hexnumber_op_Code = "1D", Background_OP_Code = "hallo", OP_Code = "red" });
        lista.Insert(20, new Lista() { Hexnumber_op_Code = "1D", Background_OP_Code = "hallo", OP_Code = "red" });
    }

    public class Lista
    {

        public string hexnumber_op_Code;

        public string Hexnumber_op_Code
        {
            get { return this.hexnumber_op_Code; }
            set
            {
                this.hexnumber_op_Code = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Hexnumber_op_Code"));
                }
            }
        }


        public string background_OP_Code;
        public string Background_OP_Code
        {
            get
            {

                if (background_OP_Code == null)
                {
                    return background_OP_Code = "White";
                }
                else
                    return this.background_OP_Code;
            }
            set
            {
                this.background_OP_Code = value;
                this.NotifyPropertyChanged("Background_OP_Code");
            }
        }
        public string oP_Code;
        public string OP_Code
        {
            get { return this.oP_Code; }
            set
            {
                this.oP_Code = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("OP_Code"));
                }
            }
        }


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

        public event PropertyChangedEventHandler PropertyChanged;
    }


    public class RelayCommand : ICommand
    {
        #region Fields

        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;

        #endregion

        #region Constructors

        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }
        #endregion

        #region ICommand Members


        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        #endregion
    }
}
}
命名空间WpfApplication5
{
/// 
///Interaktionslogik für MainWindow.xaml
/// 
公共部分类主窗口:窗口
{
公共ObservableCollection列表{get;set;}
私人中继命令cmd;
公共中继命令
{
获取{return cmd???(cmd=new RelayCommand(new Action(AddRow));}
}
公共主窗口()
{
lista=新的可观察集合{};
初始化组件();
this.DataContext=this;
}
公共void AddRow(对象对象对象)
{
整数;
Int32.TryParse(obj.ToString(),out numer);
插入(数字,新列表()
{hexnumber_op_Code=“10”,op_Code=“1”,});
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
Insert(10,new lista(){Hexnumber_op_Code=“1D”,Background_op_Code=“hallo”,op_Code=“red”});
Insert(20,new lista(){Hexnumber_op_Code=“1D”,Background_op_Code=“hallo”,op_Code=“red”});
}
公共类列表
{
公共字符串hexnumber_op_代码;
公共字符串Hexnumber_op_代码
{
获取{返回this.hexnumber_op_Code;}
设置
{
this.hexnumber\u op\u Code=值;
if(this.PropertyChanged!=null)
{
this.PropertyChanged(这是新的PropertyChangedEventArgs(“Hexnumber\u op\u Code”);
}
}
}
公共字符串背景\操作\代码;
公共字符串背景\操作\代码
{
得到
{
if(背景操作码==null)
{
返回背景\u OP\u Code=“白色”;
}
其他的
返回此.background\u OP\u代码;
}
设置
{
this.background_OP_Code=值;
此.NotifyPropertyChanged(“背景操作代码”);
}
}
公共字符串操作单元代码;
公共字符串操作单元代码
{
获取{返回this.oP_代码;}
设置
{
this.oP_代码=值;
if(this.PropertyChanged!=null)
{
this.PropertyChanged(这是新的PropertyChangedEventArgs(“OP_代码”);
}
}
}
public void NotifyPropertyChanged(字符串propName)
{
if(this.PropertyChanged!=null)
this.PropertyChanged(this,newpropertyChangedEventArgs(propName));
}
公共事件属性更改事件处理程序属性更改;
}
公共类中继命令:ICommand
{
#区域字段
只读操作_执行;
只读谓词_canExecute;
#端区
#区域构造函数
公共中继命令(操作执行)
:此(执行,空)
{
}
公共RelayCommand(操作执行,谓词canExecute)
{
if(execute==null)
抛出新的ArgumentNullException(“执行”);
_执行=执行;
_canExecute=canExecute;
}
#端区
#区域ICommand成员
公共布尔CanExecute(对象参数)
{
返回_canExecute==null?true:_canExecute(参数);
}
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
public void Execute(对象参数)
{
_执行(参数);
}
#端区
}
}
}
这应该行得通

private void Button_Click(object sender, RoutedEventArgs e)
{
    lista.Add(new Lista() { Hexnumber_op_Code = "1D", Background_OP_Code = "hallo", OP_Code = "red" });
    lista.Add(new Lista() { Hexnumber_op_Code = "1D", Background_OP_Code = "hallo", OP_Code = "red" });
}
引用自MSDN[]

ArgumentOutOfRangeException:索引小于零。 -或-索引大于计数

其中Count:集合中实际包含的元素数


因此,当您尝试在10和20处插入时,您当前的索引大于count。

@Grant Winney按钮单击()会引发问题。try-lista.add(new lista(){Hexnumber\u op\u Code=“1D”,Background\u op\u Code=“hallo”,op\u Code=“red”})@lacura是因为您试图在位置
10
和位置
20
插入一个项目……我怀疑您的收藏既没有放置该项目的位置(因此,
ArgumentOutOfRange
),因为我太笨了。。。我把值改为1,2,所有的都被播种了。。。
private void Button_Click(object sender, RoutedEventArgs e)
{
    lista.Add(new Lista() { Hexnumber_op_Code = "1D", Background_OP_Code = "hallo", OP_Code = "red" });
    lista.Add(new Lista() { Hexnumber_op_Code = "1D", Background_OP_Code = "hallo", OP_Code = "red" });
}