双向绑定需要Path或XPath。在datagrid中

时间:2014-09-16 07:33:58

标签: c# wpf datagrid

我正在使用以下函数创建DataTable:

public DataTable getTable() {

    DataTable T = new DataTable();

    T.Columns.Add("Type / Control", System.Type.GetType("System.String"));
    T.Columns.Add("Investment cost", System.Type.GetType("System.Double"));
    T.Columns.Add("Removal cost", System.Type.GetType("System.Double"));
    T.Columns.Add("Maintainance cost", System.Type.GetType("System.Double"));
    string name;

    foreach (CatalogueEntry entry in this.CatalogueList) {

        if (entry.type != EntryType.Battery) {
            name = entry.PF_type_elm.Name.GetString();
        } else {
            BatteryCatalogueEntry bce = (BatteryCatalogueEntry)entry;
            name = bce.control.Name;
        }

        DataRow row = T.NewRow();
        row[0] = name;
        row[1] = entry.InstallationCost;
        row[2] = entry.RemovalCost;
        row[3] = entry.MaintenanceCost;

        T.Rows.Add(row);
    }

    return T;
}

我把它设置为这样的数据网格:

DataTable T = getTable();
this.libraryDataGrid.ItemsSource = T.DefaultView;

第一个问题是第一列没有显示任何值(它们应该是字符串值) 当我尝试在第一列中写一些内容时,它会给出"双向绑定需要Path或XPath"错误。标记为double的列很好,可以更新。

我已经看到这个问题通过在数据结构中添加{get; set;}来解决,但我不知道这是如何应用的。

以防万一,XAML代码为:

<DataGrid x:Name="libraryDataGrid" Margin="0,0,0,30" GridLinesVisibility="Horizontal" HorizontalGridLinesBrush="Gray" Background="{x:Null}"/>

那里没有约束力。

见:“Two-way binding requires Path or XPath” when edit wpf datagrid 见:How do I fix “Two-way binding requires Path or XPath” exception in WPF Datagrids?

由于

2 个答案:

答案 0 :(得分:1)

奇怪的是解决方案是,它有效:

我更改了第一列的名称:

来自:T.Columns.Add("Type / Control", System.Type.GetType("System.String"));T.Columns.Add("Type or Control", System.Type.GetType("System.String"));

答案 1 :(得分:0)

我无法解释原因,但如果您从第一行名称(/)中删除Type / Control标记,则不会再出现错误。 如果你想将该行绑定到name,它应该是公共属性,否则你无法从视图中更改它。

相关问题