0
votes

I have a DataGrid with the following properties:

<DataGrid x:Name="dg_words" ItemsSource="{Binding}" AutoGenerateColumns="False">
                <DataGrid.Columns>  
                    <DataGridTextColumn Header="Id" Binding="{Binding id}" />
                    <DataGridTextColumn Header="word" Binding="{Binding word}" IsReadOnly="False"/>                        
                </DataGrid.Columns>
</DataGrid>

This DataGrid has two columns. First is a read-only column (Id) and second is editable (word).

I used a list to fill this DataGrid.

List<Tuple<int, string>> l = new List<Tuple<int, string>>();
l.Add(new Tuple<int, string>(1, "word 1"));
l.Add(new Tuple<int, string>(2, "word 2"));
l.Add(new Tuple<int, string>(3, "word 3"));

var l1 = (from p in l
          select new { Id = p.Item1, word = p.Item2 }).ToList();

dg_quran_words.ItemsSource = l1;

When I try to edit a cell in column word, throw an exception as:

Additional information: A TwoWay or OneWayToSource binding cannot work on the read-only property 'word' of type ...

1
The error text provides you an exact reason of the problem. Create a class to represent a list item with get and set accessors instead of anonymous onePavel Anikhouski
I've updated a comment, problem is not in xamlPavel Anikhouski

1 Answers

0
votes

Thank you Pavel Anikhouski

The problem was solved by note that Pavel commented in my question. First, add the following class:

public class c_row
{
    public int id { get; set; }
    public string word { get; set; }
}

and in the end: replace last 4 line of my code for filling the DataGrid.

var l1 = (from p in l
      select new c_row { Id = p.Item1, word = p.Item2 }).ToList();

dg_quran_words.ItemsSource = l1;