我有这门课
public class Class1
{
public int Class1Id { get; set; }
public string Description { get; set; }
}
public partial class Class2
{
public Class2()
{
this.Class3 = new HashSet<Class3>();
}
public int Id { get; set; }
public string Description { get; set; }
public Nullable<int> Class1Id { get; set; }
public virtual Class1 Class1{ get; set; }
public virtual ICollection<Class3> Class3 { get; set; }
}
public partial class Class3
{
public int Class3Id { get; set; }
public int OtherProp { get; set; }
public string Description { get; set; }
}
然后在视图模型上,我有一个 class2 的 observabel 集合
ObservableCollection<Class2> MyColl = new ObservableCollection<Class2>();
这个 Observable 绑定到一个网格,如下所示:
<DataGrid x:Name="DatGridPlanillas" ItemsSource="{Binding MyColl,Mode=TwoWay}"
<DataGrid.Columns>
<DataGridTextColumn Header="Class1 Description" Binding="{Binding /Class1.Description}"/>
<DataGridTextColumn Header="Class2 Id" Binding="{Binding Id ,Mode=TwoWay}"/>
<DataGridTextColumn Header="Class2 Descriprion" Binding="{Binding Description}"/>
</DataGrid.Columns>
</DataGrid>
我的问题是 Class1.Description never Show 并且在 VS 的结果面板中有一条消息显示
System.Windows.Data Error: 40 : BindingExpression path error: 'Class1' property not found on 'current item of collection' ''Class2_D0FAA12D4E698EE37B64760A79763EA5E957BAC04F8E9FED7BDA4B99892A712D' (HashCode=61628235)'. BindingExpression:Path=/Class1.Description; DataItem='Class2_D0FAA12D4E698EE37B64760A79763EA5E957BAC04F8E9FED7BDA4B99892A712D' (HashCode=61628235); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
所有其他属性都按我的预期呈现。
是什么导致了这个问题?