我有双数组列表。某些值是预定义的。我想将此列表绑定到DataGrid。实际上,datagrid单元格是用字典值填充的组合框。这个想法是用户从下拉列表中选择任何值,并使用双数组将相应的Key写入List。代码如下:
Dictionary<int, string> scores = new Dictionary<int, string>();
scores.Add(1, "the same");
scores.Add(3, "moderate superiority");
scores.Add(5, "strong superiority");
scores.Add(7, "very strong superiority");
scores.Add(9, "extremely superiority");
//define number of alternatives
int num = Alternatives.Children.Count - 1;
//initialize matrix for assessment scores
List<double[]> gridAssessment = new List<double[]>();
for (int i = 0; i < num; i++)
{
gridAssessment.Add(new double[num]);
}
//set initial values
for (int i = 0; i < num; i++)
{
gridAssessment[i][i] = scores.ElementAt(0).Key;
}
//define source for assessment grid
grAssessment.ItemsSource = gridAssessment;
grAssessment.AutoGenerateColumns = false;
//add columns to the grid
for (int i = 0; i < num; i++)
{
DataGridComboBoxColumn col = new DataGridComboBoxColumn();
grAssessment.Columns.Add(col);
col.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
//define source for comboboxes
col.ItemsSource = scores;
col.DisplayMemberPath = "Value";
col.SelectedValuePath = "Key";
string a = "[" + i.ToString() + "]";
Binding t = new Binding(a);
t.Mode = BindingMode.TwoWay;
col.SelectedValueBinding = t;
实际上,当我从下拉列表中选择任何值时,会出现验证标记。你能帮我解决这个问题吗?
非常感谢。
答案 0 :(得分:2)
问题在于:
//initialize matrix for assessment scores
List<double[]> gridAssessment = new List<double[]>();
for (int i = 0; i < num; i++)
{
gridAssessment.Add(new double[num]);
}
检查“输出”窗口,表示“无法转换回来”。因此,当它尝试将double转换回int时,它有一个问题。如果将此更改为int以匹配“得分”数据类型,则验证将消失。
固定代码:
//initialize matrix for assessment scores
List<int[]> gridAssessment = new List<int[]>();
for (int i = 0; i < num; i++)
{
gridAssessment.Add(new int[num]);
}