Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何以编程方式在Datatgrid WPF中绑定DataGridTextColumn中的Combobox_C#_Wpf_Xaml_Datagrid - Fatal编程技术网

C# 如何以编程方式在Datatgrid WPF中绑定DataGridTextColumn中的Combobox

C# 如何以编程方式在Datatgrid WPF中绑定DataGridTextColumn中的Combobox,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我想要的是将我的组合框加载到DataGrid的第一行,并且我的标题由复选框组成 ObservableCollection<dynamic> items = new ObservableCollection<dynamic>(); private IEnumerable<string> PropertyNames; //Store the properties names of the dynamic object //然后根据我的dt列计数创建我的标题,如附

我想要的是将我的组合框加载到DataGrid的第一行,并且我的标题由复选框组成

ObservableCollection<dynamic> items = new ObservableCollection<dynamic>();
private IEnumerable<string> PropertyNames; //Store the properties names of the dynamic object
//然后根据我的dt列计数创建我的标题,如附件所示

         for (int i = 0; i < dt.Columns.Count; i++)
            {

                DataGridTextColumn checkBoxColumn = new DataGridTextColumn();

                System.Windows.Controls.CheckBox headerCheckBox = new System.Windows.Controls.CheckBox();
                headerCheckBox.Content = "UnNamedColumn" + i;

                checkBoxColumn.Header = headerCheckBox;
                checkBoxColumn.Binding = new System.Windows.Data.Binding(dt.Rows[0][i].ToString());

                dataGrid.Columns.Add(checkBoxColumn);
            }
for(int i=0;i
//我正在尝试加载DataGrid第一行的combobox

            PropertyNames = new List<string>();
            items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == 0)
                {
                    dynamic item = new DynamicObjectClass();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        //    DataGridColumn columns = GetNewComboBoxColumn("UnNamedColumn" + j, dt.Rows[0][j].ToString(), binddt)
                        var cb = new System.Windows.Controls.ComboBox();
                        cb.ItemsSource = new List<string> { "C50", "C40", "C30" };
                        cb.SelectedItem = new System.Windows.Data.Binding(dt.Rows[0][j].ToString());
                        item.TrySetMember(new SetPropertyBinder(dt.Rows[0][j].ToString()), cb);
                    }
                    PropertyNames = item.GetDynamicMemberNames();
                    items.Add(item);
                    dataGrid.ItemsSource = items;
                    break;
                }
            }
PropertyNames=newlist();
items.Clear();
对于(int i=0;i
//最后,我在datagrid中添加了整个dt

        for (int i = 0; i < dt.Rows.Count; i++)
            {
                dynamic item = new DynamicObjectClass();
                for (int j = 0; j < dt.Columns.Count; j++)
                {  
                    item.TrySetMember(new SetPropertyBinder(dt.Rows[0][j].ToString()), dt.Rows[i][j].ToString());
                }                    
                PropertyNames = item.GetDynamicMemberNames();
                items.Add(item);
                dataGrid.ItemsSource = items;
            }
for(int i=0;i
您可以执行以下操作,并用数据替换
列表

var bindingSource = new BindingSource();
bindingSource.DataSource = new List<string> { "C50", "C40", "C30" };

comboBox1.DataSource = bindingSource.DataSource;

comboBox1.DisplayMember = new System.Windows.Data.Binding(dt.Rows[0][j].ToString());
comboBox1.ValueMember = new System.Windows.Data.Binding(dt.Rows[0][j].ToString());
var bindingSource=new bindingSource();
bindingSource.DataSource=新列表{“C50”、“C40”、“C30”};
comboBox1.DataSource=bindingSource.DataSource;
comboBox1.DisplayMember=new System.Windows.Data.Binding(dt.Rows[0][j].ToString());
comboBox1.ValueMember=new System.Windows.Data.Binding(dt.Rows[0][j].ToString());

资料来源:

到目前为止,您尝试了什么?在发布问题之前,请阅读堆栈溢出问题指南。在我修改的问题中,我展示了我在@PraveenMNo中尝试过的内容,我只希望第一行网格由组合框填充,而不是整个网格,好像现在我在组合框中给出了硬代码数据,即C50、C40、C30。一旦这可以工作,我将用我尝试过的json数据替换数据,这样会产生列转换错误,即(对于复选框,我的列类型是DataGridTextColumn,对于combobox,它需要列类型DataGridColumn)。我想这是因为您选中了
if(i==0)
,这是带有复选框的行。如果您将其更改为
i==1
,则应该执行相同的操作,但对于第二行,我已选中了If(i==0),因为我希望我的组合框位于第一行,复选框是我的列标题。
var bindingSource = new BindingSource();
bindingSource.DataSource = new List<string> { "C50", "C40", "C30" };

comboBox1.DataSource = bindingSource.DataSource;

comboBox1.DisplayMember = new System.Windows.Data.Binding(dt.Rows[0][j].ToString());
comboBox1.ValueMember = new System.Windows.Data.Binding(dt.Rows[0][j].ToString());