有没有一种方法可以将子属性显示为DataGrid中的列?

时间:2019-06-17 12:11:13

标签: c# wpf binding datagrid

我正在创建一个应用程序,并想在datagrid中显示一个包含属性和子属性的表。如何绑定子属性?

我曾尝试创建一个具有子属性绑定的列,但出现错误“ System.InvalidOperationException:'双向绑定需要Path或XPath。”您的应用已进入中断状态,但没有代码可显示,因为所有线程都在执行外部代码(通常是系统代码或框架代码)。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Context context = new Context();
        dataGrid.ItemsSource = context.Item.Local;
        dataGrid.AutoGenerateColumns = false;
        dataGrid.Columns.Add(new DataGridTextColumn() { Header = "Dispositivo", Binding = new Binding("Movie.Device") });
    }
}

public class Context : DbContext
{
    public Context():base("data source=desktop-ve11ljk;initial catalog=prueba1;integrated security=true")
    {

    }
    public DbSet<Item> Item { get; set; }
    public DbSet<Movie> Movie { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Item>().HasOptional(i => i.Movie).WithRequired(i => i.Item);
    }
}

public partial class Item
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }        
    [StringLength(10)]
    public string Title { get; set; }

    public Movie Movie { get; set; }
}

public partial class Movie
{
    [Key,DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ID { get; set; }
    [StringLength(20)]
    public string Device { get; set; }

    public Item Item { get; set; }
}

0 个答案:

没有答案