【问题标题】:Nested Databinding to Gridview嵌套数据绑定到 Gridview
【发布时间】:2015-11-25 08:49:34
【问题描述】:

我正在尝试将嵌套项数据绑定到我的 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 路径错误:在“System.Collections.Generic.List1[[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.List1[[Studies.API.StudentSearchData, Name, Version=1.0.0.0, Culture=neutral, PublicKeyToken= 上找不到“Name”属性空]],mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=7cec85d7bea7798e';目标元素是 'Windows.UI.Xaml.Documents.Run' (Name='null');目标属性是“文本”(类型“字符串”)

【问题讨论】:

    标签: c# xaml gridview binding


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 2012-06-17
      • 1970-01-01
      相关资源
      最近更新 更多