2017-05-16 8 views
-1

これは初めてのWPFとC#です。現在は完全に空白です。私はstackoverflowとインターネット上で無数の投稿を行ったが、実際には解決策を見つけることができませんでした。私は解決策を見つけたに違いないが、正確に理解することはできないかもしれない。ILIST to XAMLをバインドします

問題: 私は、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データは次のようになります。私は単にそれが0のインデックスで教員のユーザー名を返していreturn person[0].usernameとき

"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": "" 
     },{...}] 

今問題があります。しかし、私はこのリストを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; 
      } 
     } 
+0

なぜ「IList」ですか?あなたはIListで何も保存していない 'List 'を作成するだけです。次にListViewを使って、あなたがXAMLで試していたものを表示できますか? 'ObservableCollection <>'は、コレクションの変更を見たい場合には別の方法です。 – mvermef

+0

私は教員とその詳細をそれぞれ表示しようとしています:例:教員の名前:彼/彼女の名前の下にabc彼の興味や写真などを示したい。私はXAMLに何も表示できません。 –

+0

ok master/details – mvermef

答えて

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); 
     } 
    } 

} 

ラフが、コレクションとの選択にどのように対処するかについて、あなたにポイントを取得します。

+0

男!!!!これはちょうど5分前に起こったものです。あなたは主人公です! :D –

-1

表示する文字列を1つだけ戻すことができる場合は、すべてのデータを連結する必要があります。

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

これは私がルーカスを探しているわけではありません。 –

関連する問題