将ILIST绑定到XAML

时间:2017-05-16 04:19:00

标签: c# wpf xaml mvvm data-binding

这是我第一次使用WPF和C#,到目前为止我完全是空白。我确实在stackoverflow和互联网上经历了无数的帖子,但实际上找不到解决方案。可能我必须遇到解决方案,但无法完全理解。

问题: 我使用HttpClient从api获取数据。我成功地反序列化了这些数据,并且我能够将它绑定在前端(XAML),但问题是它是IList,我只能在列表中显示1行。我想迭代该列表并取出个别数据。

public class FacultyViewModel
    {
        public Faculty faculty = new Faculty();
        IList<Faculty> person;
        public FacultyViewModel()
        {
            string jsonResponse = WebService.getData(Faculty.url);
            JObject o = JObject.Parse(jsonResponse);
            JArray a = (JArray)o["faculty"];
            person =  a.ToObject<IList<Faculty>>();
        }
        public IList<Faculty> List
        {
            get
            {
                return person;
            }
        }
    }

JSON DATA看起来像这样:

 "faculty": [
        {
            "username": "blah",
            "name": "Some name",
            "tagline": "",
            "imagePath": "some image",
            "title": "Lecturer",
            "interestArea": "Some Interest",
            "office": "Some Office",
            "website": "",
            "phone": "Some number",
            "email": "email",
            "twitter": "",
            "facebook": ""
        },
        {
            "username": "abcd",
            "name": "EFG",
            "tagline": "",
            "imagePath": "image.jpg",
            "title": "Assistant Professor",
            "interestArea": "interests here",
            "office": "office",
            "website": "website",
            "phone": "phone",
            "email": "email",
            "twitter": "",
            "facebook": ""
        },{...}]

现在的问题是,当我只是return person[0].username它确实返回了0索引的教师用户名。但是我想在我的XAML前端视图中迭代这个列表。我怎么能够?当我遇到它时,我确实尝试过itemscontrol和很多东西,但没有找到有效的解决方案。

我只想在XAML文件内的前端迭代这个列表。像这样迭代:

for(i=0; i<person.length; i++)
{
  display person[i].username
  display person[i].email and so on....
}

      public IList<Faculty> List
      {
          get
          {
             return person;
          }
      }

2 个答案:

答案 0 :(得分:0)

<Page.DataContext>
  <vm:FacultyViewModel x:Name="ViewModel" />
</Page.DataContext>
<!-- Master --> 
<Grid>
  <Grid.ColumnDefinitions>
      <ColumnDefinition Width="100" />
      <ColumnDefinition />
  </Grid.ColumnDefinitions>
<ListView ItemSource="{Binding FacultyList}"  Grid.Column="0" 
     SelectedItem="{Binding SelectedFaculty, Mode=TwoWay}" >
   <ListView.ItemTemplate>
       <DataTemplate>
           <Grid>
             <TextBlock Text="{Binding Name}" />
           </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
 </ListView>

 <!-- Details -->
 <Grid Grid.Column="1"
   <Grid.RowDefinitions>
     <RowDefinition />
     <RowDefinition />
     <RowDefinition />
     <RowDefinition />
   </Grid.RowDefinitions>
   <TextBlock Text="{Binding SelectedPerson.name}" />
   <TextBlock Text="{Binding SelectedPerson.username }" Grid.Row="1" />
   <TextBlock Text="{Binding SelectedPerson.title}" Grid.Row="2" />
</Grid>

 //View Code-Behind
public partial class FacultyViewModel : INotifyPropertyChanged
{
     public event PropertyChangedEventHandler PropertyChanged;


     private Person _person;
     private List<Person> _facultyList;

     public FacultyViewModel(){

        InitializeComponents();

        DataContext = new FacultyViewModel();

        //list will not get renewed till you restart app
        //any additions to the list while running will not be shown.
        _facultyList = a.ToObject<List<Faculty>>();
     }


     // includes your other stuff here....



     //Selected a Faculty member from the listview.
     public Person SelectedPerson {
          get{ return _person;}
          set{ _person = value;
               RaisePropertyChange(()=>SelectedPerson);
         }
     }

    // list to populate the listview
    public List<Person> FacultyList{
         get{ return _facultyList;}
    }

     public virtual void RaisePropertyChange([CallerMemberName] string propertyName = null){
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName)));
     }

   public void RaisePropertyChange<TProperty>(Expression<Func<TProperty>> property){
        RaisePropertyChange(property.GetMemberInfo().Name);
   }

   protected void OnPropertyChanged(PropertyChangedEventArgs e){
      var handler = PropertyChanged;
      if(handler != null){
         handler(this,e);
      }
  }

}

粗略但有关如何处理收藏和选择的问题。

答案 1 :(得分:-1)

如果只能返回1个字符串进行显示,则必须连接所有数据。

var str = "";
foreach(var e in person){
    str += e.username+"\n";
    str += e.email+"\n";
    ...
}
return str;