开发者

DataGrid 'EditItem' is not allowed for this view when dragging multiple items

开发者 https://www.devze.com 2023-03-26 04:50 出处:网络
I have a datagrid which gets data like this: public struc开发者_开发技巧t MyData { public string name { set; get; }

I have a datagrid which gets data like this:

    public struc开发者_开发技巧t 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:

System.InvalidOperationException was unhandled

Message: 'EditItem' is not allowed for this view.

Why is this? Is it because of the way the data is entered?

Any ideas?

Thanks!


I got this issue when assigning ItemsSource to IEnumerable<T>.

I fixed it by converting the IEnumberable<T> to a List<T> and then assigning that to ItemsSource.

I'm not sure why using IEnumerable caused that issue, but this change fixed it for me.


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


If you use datagrid DataGridCheckBoxColumn you need to set <Setter Property="IsEditing" Value="true" /> on check box column. See this: https://stackoverflow.com/a/12244451/1643201


This answer is not my own, just the working code example suggested by AnthonyWJones.

public class MyData //Use class instead of struct
{
    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"}));

//Create a list of MyData instances
List<MyData> myDataItems = new List<MyData>(); 
myDataItems.Add(new MyData() { name = "Song1", artist = "MyName", location =     "loc"});
myDataItems.Add(new MyData() { name = "Song2", artist = "MyName", location =     "loc2"});

//Assign the list to the datagrid's ItemsSource
dataGrid1.ItemsSource = items;


For my case,

processLimits.OrderBy(c => c.Parameter);

returns an

IOrderedEnumerable<ProcessLimits> 

not a

List<ProcessLimits>

so when I assign a style for my event setter to a checkbox column in my datagrid

style.Setters.Add(new EventSetter(System.Windows.Controls.Primitives.ToggleButton.CheckedEvent, new RoutedEventHandler(ServiceActiveChecked)));

ServiceActiveChecked is never called and I got

'EditItem' is not allowed for this view.

and for anyone else doing checkboxes in datagrid columns, I use a column object with my column data in this constructor for adding the data grid I use with adding the style above.

datagridName.Columns.Add(new DataGridCheckBoxColumn()
                            {
                                Header = column.HeaderText.Trim(),
                                Binding = new System.Windows.Data.Binding(column.BindingDataName.Trim()) { StringFormat = column.StringFormat != null ? column.StringFormat.Trim().ToString() : "" },
                                IsReadOnly = column.IsReadOnlyColumn,
                                Width = new DataGridLength(column.DataGridWidth, DataGridLengthUnitType.Star),
                                CellStyle = style,
                            });


I solved this by setting the datagrid's source after the InitializeComponent:

    public MainWindow()
    {
        InitializeComponent();
        FilterGrid.ItemsSource = ScrapeFilter;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消