wpf DataGrid - “双向绑定需要 Path 或 XPath。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22595101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:18:40  来源:igfitidea点击:

DataGrid - "Two-way binding requires Path or XPath."

c#wpfxpathdatagrid

提问by Lau

I would like to display on DataGrid my object database

我想在 DataGrid 上显示我的对象数据库

public class Student
{
public string Imie { get; set; }
public string Nazwisko { get; set; }
string Numer { get; set; }

internal List<Telefon> Telefony { get; set; }
internal Adres Adres { get; set; }
}

In Adresand Telefonclass I have obviously some extra fields.

AdresTelefon课程中,我显然有一些额外的字段。

My XAML:

我的 XAML:

<DataGrid Name="dataGrid" ItemsSource="{Binding Student}" AutoGenerateColumns="False" CellEditEnding="dataGrid_CellEditEnding" CurrentCellChanged="dataGrid_CurrentCellChanged" PreviewKeyDown="dataGrid_PreviewKeyDown">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Imie"        Binding="{Binding Imie}"/>
            <DataGridTextColumn Header="Nazwisko"    Binding="{Binding Nazwisko}"/>
            <DataGridTextColumn Header="Numer"       Binding="{Binding Numer}"/>
            <DataGridTextColumn Header="Ulica"       Binding="{Binding Adres.Ulica}"/>
            <DataGridTextColumn Header="KodPocztowy"       Binding="{Binding  Adres.KodPocztowy}"/>
            <DataGridTextColumn Header="Miasto"       Binding="{Binding Adres.Miasto}"/>
            <DataGridTextColumn Header="Tel. Numer"       Binding="{Binding Telefon.Numer}"/>
            <DataGridTextColumn Header="Tel. Operator"       Binding="{Binding Telefon.Operator}"/>
        </DataGrid.Columns>
    </DataGrid>

I can easily get and set Imie, Nazwiskoand Numerfields but when i'm trying to set value of Ulica(field in Adresclass) compiler gives me this exception:

我可以轻松获取和设置ImieNazwiskoNumer字段,但是当我尝试设置Ulica 的值(Adres类中的字段)时,编译器给了我这个异常:

InvalidOperationException was unhandled
Two-way binding requires Path or XPath.

Thanks for help.

感谢帮助。

回答by Rohit Vats

I suspect Adresbound property is nullso when you try to edit the column value for binding Binding="{Binding Adres.Ulica}", it tries to set value for Adres.Ulica but Adres itself was null. Hence binding fails silently on load.

我怀疑Adresbound 属性为 null,因此当您尝试编辑 binding 的列值时Binding="{Binding Adres.Ulica}",它会尝试为 Adres.Ulica 设置值,但 Adres 本身为 null。因此,绑定在加载时会静默失败。

You have to make sure Adresis initialized for all binded objectsso that you can edit the value for it's child property Ulicafrom dataGrid.

您必须确保Adres为所有绑定对象进行了初始化,以便您可以Ulica从 dataGrid编辑其子属性的值。

回答by dkozl

In your view model both Telefonyand Adresproperties are declared as internal. Try changing these properties to public. Check for Binding Source Types

在您的视图模型中,TelefonyAdres属性都声明为internal. 尝试将这些属性更改为public. 检查绑定源类型

You can bind to public properties, sub-properties, as well as indexers

您可以绑定到公共属性、子属性以及索引器

You've also mentioned that you can get Numerto work but in the sample code it appears to be declared as privatewhich is not valid binding source.

您还提到您可以开始Numer工作,但在示例代码中,它似乎被声明为private无效的绑定源。

回答by Syed

The problem is because of the .(dot) in the binding member name. Please refer to: Binding to fields containing a period in DataTable in C#/WPF

问题在于绑定成员名称中的 .(dot)。请参考: 绑定到C#/WPF中DataTable中包含句点的字段