Reputation: 1432
I need to make my DataGrid column editable, but can't figure out how to make this. When I try to edit column I catch an exception "EditItem is not allowed for this view".
My XAML:
<DataGrid IsReadOnly="False" AutoGenerateColumns="False" Margin="6,6,5,18" Name="dataGrid1" ItemsSource="{Binding MyDictionary}" CellEditEnding="dataGrid1_editCells">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Header="Name" Binding="{Binding Key}" />
<DataGridTextColumn IsReadOnly="False" Header="Value" Binding="{Binding Value}" />
</DataGrid.Columns>
</DataGrid>
And .cs:
public partial class MyView : Window
{
private Dictionary<string, string> myDictionary = new Dictionary<string, string>();
public Dictionary<string, string> Dictionary { get { return myDictionary ; } set { myDictionary = value; } }
public MyView()
{
// Here is some code that fills dictionary
InitializeComponent();
this.DataContext = this;
}
}
What is the problem? How can I make my second column editable?
Upvotes: 1
Views: 1499
Reputation: 2246
DataGrid works with collections and Dictioanry is a collection of KeyValuePair. If you look at this struct you'll see you that it's not mutable.
Upvotes: 0
Reputation: 184296
If you bind to a dictionary it enumerates the contents as KeyValuePair
instances, which is a struct. You cannot edit the members of a struct keeping the same instance (and the properties are get-only in this case).
Bind to a list of class instances instead.
Upvotes: 2