简体   繁体   English

如果AutoGenerateColumns为false,则DataGrid无法正确绑定到DataGrid

[英]DataGrid does not properly bind to DataGrid if AutoGenerateColumns is false

I've been driving myself nuts with this and I'm hoping I'm just missing something obvious. 我一直在为此疯狂,我希望我只是缺少一些明显的东西。

I have a DataGrid I want to bind to an in-memory DataTable . 我有一个要绑定到内存中的DataTableDataGrid With AutoGenerateColumns set to true, I can edit the fields and have the changes propagate to the datatable and everything is great. 在AutoGenerateColumns设置为true的情况下,我可以编辑字段并将更改传播到数据表,并且一切都很好。 The problem is that I wanted to have a Drop-down combobox for the 'Status' field. 问题是我想为“状态”字段提供一个下拉组合框。 I then set AutoGenerateColumns to false and manually added the column headers in XAML. 然后,我将AutoGenerateColumns设置为false并在XAML中手动添加列标题。 When I do this I can no longer edit the fields and have the changes persist, instead each field is blank after I click away. 执行此操作后,我将无法再编辑字段并使更改保持不变,而是单击后将每个字段都保留为空白。 I checked the status of the Datagrid columns during debugging and found that the bindings are reading as null. 我在调试过程中检查了Datagrid列的状态,发现绑定读为null。 Any ideas of what I've done wrong? 关于我做错了什么的任何想法?

XAML Code: XAML代码:

<DataGrid Margin="31,29,0,29" Name="dgTickets" RowEditEnding="dgTickets_RowEditEnding" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID #" x:Name="ID" />
            <DataGridTextColumn Header="Customer Name" x:Name="CustomerName" />
            <DataGridTextColumn Header="Phone #" x:Name="Phone"/>
            <DataGridComboBoxColumn Header="Status" x:Name="Status"/>
            <DataGridCheckBoxColumn Header="Bool?" x:Name="bool1"/>
            <DataGridCheckBoxColumn Header="Bool?" x:Name="bool2"/>
            <DataGridCheckBoxColumn Header="Bool?" x:Name="bool3"/>
            <DataGridTextColumn Header="Notes" x:Name="Notes"/>
        </DataGrid.Columns>
    </DataGrid>

My C# code for creating the DataTable and associating it: 用于创建DataTable并将其关联的C#代码:

    //add columns
        column = new DataColumn("ID", System.Type.GetType("System.String")); //two piece constructor - adds header string and data type
        dbTable.Columns.Add(column); //adds the column to the table

        column = new DataColumn("CustomerName", System.Type.GetType("System.String"));
        dbTable.Columns.Add(column);

        column = new DataColumn("Phone#", System.Type.GetType("System.String"));
        dbTable.Columns.Add(column);

        column = new DataColumn("Status", System.Type.GetType("System.String"));

        dbTable.Columns.Add(column);

        column = new DataColumn("bool1", System.Type.GetType("System.Boolean"));
        dbTable.Columns.Add(column);

        column = new DataColumn("bool2", System.Type.GetType("System.Boolean"));
        dbTable.Columns.Add(column);

        column = new DataColumn("bool3", System.Type.GetType("System.Boolean"));
        dbTable.Columns.Add(column);

        column = new DataColumn("Notes", System.Type.GetType("System.String"));
        dbTable.Columns.Add(column);
        //add rows
        row = dbTable.NewRow(); 

        dbTable.Rows.Add(row); 

        dbTable.AcceptChanges();
        dgTickets.ItemsSource = dbTable.DefaultView;

Any advice is appreciated. 任何建议表示赞赏。 I've tried several different ways to get these two elements to work together, but I can't seem to get two to bind properly if the headers are manually defined. 我尝试了几种不同的方法来使这两个元素协同工作,但如果头是手动定义的,我似乎无法正确绑定两个元素。

我认为您只需要在其中扔绑定,例如:

<DataGridTextColumn Header="Customer Name" x:Name="CustomerName" Binding={Binding path=CustomerName} />

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

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