简体   繁体   English

WPF将字典绑定到组合框

[英]WPF Bind Dictionary to combobox

I have the List of double Arrays. 我有双数组列表。 Some values are predefined. 一些值是预定义的。 And I want to bind this list to DataGrid. 我想将此列表绑定到DataGrid。 Actually datagrid cells are comboboxes filled with Dictionary Values. 实际上,datagrid单元是充满字典值的组合框。 The idea is that user selects any value from dropdown and appropriate Key is written to List with double Arrays. 这个想法是用户从下拉菜单中选择任何值,并将适当的Key写入带有Double Arrays的List中。 Code is the following: 代码如下:

        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;  

Actually when I select any value from dropdown validation mark appears. 实际上,当我从下拉验证标记中选择任何值时都会出现。 Could you please assist me with this binding? 你能帮我这个约束吗?

Thanks a lot. 非常感谢。

The issue resides here: 问题出在这里:

//initialize matrix for assessment scores
List<double[]> gridAssessment = new List<double[]>();
for (int i = 0; i < num; i++)
{
    gridAssessment.Add(new double[num]);
}

Checking the Output window, it says it "cannot convert back". 检查“输出”窗口,它说“无法转换回”。 So when it tries to convert a double back to an int, it has a problem. 因此,当它尝试将双精度型转换回int时,就会出现问题。 If you change this to int to match the "scores" data type, the validation will be gone. 如果将其更改为int以匹配“分数”数据类型,则验证将消失。

Fixed Code: 固定代码:

//initialize matrix for assessment scores
List<int[]> gridAssessment = new List<int[]>();
for (int i = 0; i < num; i++)
{
    gridAssessment.Add(new int[num]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM