DataGrid - “双向绑定需要路径或XPath”。 [英] DataGrid - "Two-way binding requires Path or XPath."

查看:455
本文介绍了DataGrid - “双向绑定需要路径或XPath”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Adres Telefon 类我明显有一些额外的字段。

In Adres and Telefon class I have obviously some extra fields.

我的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>

我可以轻松获取并设置 Imie Nazwisko 数组字段,但是当我尝试设置 Ulica Adres 类中的字段)的值时,编译器会给我这个例外: / p>

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.

感谢您的帮助。

推荐答案

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

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


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

您还提到您可以获得数组,但在示例代码中被声明为 private 这是无效的绑定源。

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.

这篇关于DataGrid - “双向绑定需要路径或XPath”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆