嵌套数据绑定到Gridview

时间:2015-11-25 08:49:35

标签: c# xaml gridview binding

我试图将嵌套项目数据绑定到Gridview上。我对顶级项目没有任何问题,只有嵌套的那些(主题名称)我无法访问它。这是我目前的代码。

studentSearch.xaml.cs

public class StudentSearchData : INotifyPropertyChanged
{
private string studentName;
public string StudentName
{
    get
    {
        return studentName;
    }
    set
    {
        if (studentName != value)
        {
            studentName = value;
            OnNotifyPropertyChanged("StudentName");
        }
    }
}

public List<Subject> Subjects { get; set; }

public event PropertyChangedEventHandler PropertyChanged;

private void OnNotifyPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
}

public class Subject
{
public string Name { get; set; }
}

studentSearch.Xaml

<GridView Name="studentGridView" ItemSource="{Binding StudentName">
<Grid.RowDefinitions>
     <RowDefinition Height="auto" />
     <RowDefinition Height="auto" />
 </Grid.RowDefinitions>

 <DataTemplate>
  <TextBlock Text="{Binding studentName}" Grid.Row="0">
  <StackPanel Grid.Row="1">
      <TextBlock Text="{Binding Name}" Grid.Row="0">
   </StackPanel>
  </DataTemplate>
</GridView>

我在尝试访问主题名称时收到的错误消息是

  

错误:BindingExpression路径错误:&#39;名称&#39;未在&#39; System.Collections.Generic.List上找到属性1[[Studies.API.StudentSearchData , Studies, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. BindingExpression: Path='Name' DataItem='System.Collections.Generic.List 1 [[Studies.API.StudentSearchData,Name,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]],mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e&#39 ;;目标元素是&#39; Windows.UI.Xaml.Documents.Run&#39; (名称=&#39;空&#39);目标属性是&#39; Text&#39; (键入&#39; String&#39;)

1 个答案:

答案 0 :(得分:0)

您的GridView正在使用ItemSource&#39; StudentName&#39;。尝试将其更改为StudentSearchData和XAML,因为Subject是一个列表,您需要通过调用它来访问它Subject.Name