1

I want my DataGrid in WPF show nested classes into my objects. I'm using NHibernate to retrieve data.

My Model class:

public class PublishedBook
{
    public virtual long ID { get; set; }
    public virtual string Title { get; set; }
    public virtual YearSemester BookYearSemester { get; set; }

    public override string ToString()
    {
        return string.Format("{0} ({1})", Title, BookYearSemester);
    }
}
public class YearSemester 
{
    public virtual long ID { get; set; }
    public virtual string Description { get; set; }

    public override string ToString()
    {
        return Description;
    }
}

My Repository class:

public class PublishedBookRepository : BaseRepository
{
    public PublishedBookRepository(ISession objCurrentSession) :
        base(objCurrentSession) { }

    public IEnumerable<PublishedBook> GetAllPublishedBooks()
    {
        return base.CurrentSession
            .CreateCriteria<PublishedBook>()
            .List<PublishedBook>();
    }
}

My XAML Code:

<DataGrid x:Name="dagtagridPublishedBooks" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Title" Binding="{Binding Title}" />
        <DataGridTextColumn Header="YearSemester" Binding="{Binding YearSemester}" />
    </DataGrid.Columns>
</DataGrid>

In the second column, the data isn't displayed. Anyone knows why? Thanks, guys!!

4

2 回答 2

1

第二列的绑定路径应该是属性名:BookYearSemester,而不是类名YearSemester。尝试:

<DataGrid x:Name="dagtagridPublishedBooks" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Title" Binding="{Binding Title}" />
        <DataGridTextColumn Header="YearSemester" Binding="{Binding BookYearSemester}" />
    </DataGrid.Columns>
</DataGrid>
于 2012-08-09T19:55:24.810 回答
0

因为YearSemester是一个类,所以您可以使用完整路径来显示其属性,例如BookYearSemester.ID或,或者使用将被替换为类对象BookYearSemester.Description的自定义模板DataTemplate

于 2013-01-22T09:59:39.340 回答