【问题标题】:Two-way binding requires Path or XPath. in datagrid双向绑定需要 Path 或 XPath。在数据网格中
【发布时间】:2014-11-09 20:45:17
【问题描述】:

我正在创建一个具有以下功能的数据表:

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;

第一个问题是第一列没有显示值(应该是字符串值) 当我尝试在第一列中写一些东西时,它会给出“双向绑定需要路径或 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?

谢谢

【问题讨论】:

  • 向我们展示您的 XAML 标记
  • 标记?你是说 XAML 吗?
  • @user3020849 XAML 中的 M 代表 markup
  • 我添加了用于数据网格的非常简单的 XAML 代码。

标签: c# wpf datagrid


【解决方案1】:

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

我更改了第一列的名称:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-11-29
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2014-11-29
      • 2011-10-29
      相关资源
      最近更新 更多