I would like to display on DataGrid my object database
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 class I have obviously some 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 , Nazwisko and Numer fields but when i'm trying to set value of Ulica (field in Adres class) compiler gives me this exception:
InvalidOperationException was unhandled
Two-way binding requires Path or XPath.
Thanks for help.
I suspect Adres
bound property is null so 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.
You have to make sure Adres
is initialized for all binded objects so that you can edit the value for it's child property Ulica
from dataGrid.
In your view model both Telefony
and Adres
properties are declared as internal
. Try changing these properties to public
. Check for Binding Source Types
You can bind to public properties , sub-properties, as well as indexers
You've also mentioned that you can get Numer
to work but in the sample code it appears to be declared as private
which is not valid binding source.
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
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.