使用ComboboxColumns将DataGrid绑定到List

时间:2015-11-16 21:33:20

标签: c# wpf data-binding datagrid

我有一个词典,其中的值用作DataGrid中的comboboxColumn的源。任务如下:当用户从组合框中选择适当的项时,不应将值写入列表。 我的代码:

        Dictionary<double, string> scores = new Dictionary<double, 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

        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;  
            t.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;              
            col.SelectedValueBinding = t;                   
        } 

你能帮我装订一下吗?因为所选值的键未存储在列表中:(

2 个答案:

答案 0 :(得分:0)

我使用相同的代码并尝试重新创建UI

代码隐藏

列表与LT;加倍[] GT; gridAssessment = new List&lt; double []&gt;();

    public MainWindow()
    {
        InitializeComponent();

        Dictionary<double, string> scores = new Dictionary<double, 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 = 3;//Alternatives.Children.Count - 1;

        //initialize matrix for assessment scores

        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;
            t.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            col.SelectedValueBinding = t;
        } 

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        StringBuilder str1 = new StringBuilder();

        foreach (var c in gridAssessment)
        {
            str1.Append("<");
            foreach (var item in c)
            {
                str1.AppendFormat(" {0} ", item);    
            }
            str1.Append(">");
        }
        val.Content = str1.ToString();
    }

XAML

<Grid HorizontalAlignment="Left" VerticalAlignment="Top">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <DataGrid Name="grAssessment" />
    <Button Content="Check" Grid.Row="1" Click="Button_Click" />
    <Label Name="val" Grid.Row="2"  />
</Grid>

我看到显示的值为1,2,3。如果需要设置字符串值,则可能需要更改List&lt; double []&gt; gridAssessment = new List&lt; double []&gt;() 如列表&lt; string []&gt; gridAssessment = new List&lt; string []&gt;(); 并使用SelectedValuePath反转组合的DisplayMemberPath。

希望这有帮助!

答案 1 :(得分:0)

我将Dictionary改为ObservableCollection,现在它可以工作了。