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

前端 未结 6 1359
一向
一向 2021-01-05 08:53

I have a datagrid which gets data like this:

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


        
相关标签:
6条回答
  • 2021-01-05 09:33

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

        public MainWindow()
        {
            InitializeComponent();
            FilterGrid.ItemsSource = ScrapeFilter;
        }
    
    0 讨论(0)
  • 2021-01-05 09:41

    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;
    
    0 讨论(0)
  • 2021-01-05 09:45

    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

    0 讨论(0)
  • 2021-01-05 09:48

    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,
                                });
    
    0 讨论(0)
  • 2021-01-05 09:51

    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.

    0 讨论(0)
  • 2021-01-05 09:56

    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

    0 讨论(0)
提交回复
热议问题