The DataGrid binding Dictionary problem

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

[The problem]

      DataGridCheckBoxColumn binding using DataGrid in a recent project of the IsSelected field background TagModel class, the data source is the TagModel type of Dictionary, the operation that Checkbox can not normal check, an error: this view is not allowed“EditItem”.

[To reproduce]

Reception:

<DataGridCheckBoxColumn Binding="{Binding IsSelected}" />

Backstage:

Dictionary<string, TagModel> dicTag = new Dictionary<string, TagModel>();
TagModel tm = new TagModel ();
dicTag.Add("Test",tm);
dgTest.ItemsSource = dicTag.Values;

TagModel.cs:

private bool isSelected = false; 
public bool IsSelected
{
            get
            {
                return isSelected;
            }

            set
            {
                isSelected = value;
            }
 }

To run after the click CheckBox will pop up error: this view is not allowed“EditItem”.




[Problem analysis]

The 1 amendment to the code:

<DataGridTemplateColumn>                                

<DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding IsSelected}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

The test is no problem.

2 do not modify the code, modify the background code

Dictionary<string, TagModel> dicTag = new Dictionary<string, TagModel>();
TagModel tm = new TagModel ();
dicTag.Add("Test",tm);
List<TagModel> lstTM = new List<TagModel>(dicTag.Values);
dgTest.ItemsSource = lstTM ;

The test is no problem.

Analysis: 1 using Dictionary Values as the data source, DataGridTemplateColumn can be normal operation while the DataGridCheckBoxColumn will be in error, guess which of the two Column uses a different processing mechanism.

        2 use List to copy the Dictionary Values as a data source, DataGridTemplateColumn and DataGridCheckBoxColumn can be normal operation, Because Dictionary Values does not inherit the IList interface and List inherits the interface, Guess IList is not inherited in the variables used as data source processing mechanism of DataGridCheckBoxColumn and DataGridTemplateColumn in different.


[Problem solving]

     By MSDN moderator Lisa Zhu explained, finally understand the problem

[Summary]

       The use of Dictionary Values as a data source needs to be Dictionary Values copied to List, then List as a data source binding.


[P.S]

       If you use the Dictionary Values as the data source and use the CellEditingTemplate binding, can be the IsReadOnly of the column is set to True, it will not use the CellEditingTemplate attribute value, does not throw an error. But at the same time also cannot operate this column.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Darren at November 12, 2013 - 12:16 PM