简体   繁体   中英

Two-way binding requires Path or XPath. in datagrid

I am creating a DataTable with the following function:

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;
}

and I set it to a data grid like this:

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

The first problem is that the first column shows no values (they should be string values) and when I try to write something in the first colum it gives the "Two-way binding requires Path or XPath" error. The columns marked as double are fine and they are updatable.

I have seen this question being solved by putting {get;set;} in the data structures but I don't see how that aplies here.

Just in case, the XAML code is:

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

There is no binding there.

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

Thanks

By bizarre the solution is, it works:

I changed the name of the first column:

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

I can't explain why but if you remove the / mark from the first rows name( Type / Control ),you won't get the error anymore. And if you want to bind that row to name ,it should be a public property otherwise you can't change it from view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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