DataGrid - "Bilateral binding requires Path or XPath."

I would like to display my object database in a 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 Adres and Telefon , I obviously have a few extra fields.

My 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 , Nazvski and Number , but when I try to set the value to Ulica (field in the Adres class) the compiler gives me this exception:

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

Thanks for the help.

+1
source share
3 answers

I suspect that the Adres bound property is null , so when you try to change the column value for the Binding="{Binding Adres.Ulica}" binding, it tries to set the value for Adres.Ulica, but Adres itself was zero. Therefore, the binding fails at boot.

You need to make sure that Adres initialized for all objects with a binding so that you can edit the value of its child Ulica property from dataGrid.

+1
source

In your view model, Telefony and Adres declared as internal . Try changing these properties to public . Check Types of Binding Sources

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

You also mentioned that you can get Numer to work, but in the code example, it appears as private , which is not a valid binding source.

+1
source

The problem is. (period) in the name of the anchor element. Please refer to: Linking to fields containing a period in a DataTable in C # / WPF

+1
source

Source: https://habr.com/ru/post/970930/


All Articles