本文介绍了拖动多个项目时,此视图不允许使用DataGrid'EditItem'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagrid获取这样的数据:

  public struct MyData 
{
public字符串名称{set;得到; }
public string artist {set;得到; }
public string location {set;得到; }
}

DataGridTextColumn col1 = new DataGridTextColumn();
col4.Binding = new Binding(name);
dataGrid1.Columns.Add(col1);
dataGrid1.Items.Add((new MyData(){name =Song1,artist =MyName,location =loc}));
dataGrid1.Items.Add((new MyData(){name =Song2,artist =MyName,location =loc2}));

问题是 - 每当用户尝试编辑单元格或拖动多个单元格时 - 应用程序会抛出一个例外:

为什么会这样?是因为输入数据的方式吗?

任何想法?

谢谢!

解决方案

而不是使用 struct 而是使用



UPDATED ANSWER:尝试将MyData实例添加到列表,然后将该列表分配给DataGrid.ItemsSource


I have a datagrid which gets data like this:

    public struct MyData
    {
        public string name { set; get; }
        public string artist { set; get; }
        public string location { set; get; }
    }

    DataGridTextColumn col1 = new DataGridTextColumn();
    col4.Binding = new Binding("name");
    dataGrid1.Columns.Add(col1);
    dataGrid1.Items.Add((new MyData() { name = "Song1", artist = "MyName", location =     "loc"}));
    dataGrid1.Items.Add((new MyData() { name = "Song2", artist = "MyName", location =     "loc2"}));

The problem is- whenever a user tries to edit a cell or drags multiple cells- the app throws an exception:

Why is this? Is it because of the way the data is entered?
Any ideas?
Thanks!

解决方案

Instead of using a struct use a class instead.

UPDATED ANSWER: Try adding your MyData instances to a List then assigning that list to the DataGrid.ItemsSource

这篇关于拖动多个项目时,此视图不允许使用DataGrid'EditItem'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:19