2016-08-15 10 views
0

リスト<から1つのアイテムをバインドする際に問題があります。XAMLバインディングUWPのList <>アイテム

私はJSONファイルからデータをインポートしました。その中に配列を通してTextBoxに表示したいと思います。

データは次のようになります。

public class A 
{ 
    public string Str { get; set; } 
} 
public class ParentA 
{ 
    public string Name { get; set; } 
    public List<A> Items { get; set; } 
} 

public class MyPage : Page 
{ 
    public ParentA ObjectParrentA { get; set; } 
    public int SelectedItem { get; set; } 

    public MyPage() 
    { 
     ObjectParrentA = new ParentA(); 
     // here is binding of ObjectParrentA by JSON data. 
     SelectedItem = 0; 
     this.DataContext = this; 
     this.InitializeComponent(); 
    } 
    private void NextItem_Click(object sender, RoutedEventArgs e) 
    { 
     SelectedItem++; 
    } 
} 

とXAML:ここ

{name: "ABC", Items: [ {str: "aaa"}, {str: "bbb"}, {str: "ccc"} ]} 

は、C#の例で示されるべき

<TextBlock Text="{Binding ObjectParrentA.Items[SelectedItem].Str}" /> 
<Button Name="NextItem" Click="NextItem_Click" Content="Next Item" /> 

のTextBoxでの開始時に " aaa "をクリックした後、[次の項目]ボタンをクリックすると" bbb "などが表示されます。

これで、TextBlockは空白になりました。 "Binding ObjectParrentA.Items [SelectedItem] .Str"に問題があるはずですが、バインディングの経験はあまりありません。

これを達成するためのヒントはありますか?ありがとうございました。

答えて

1

もう少しMVVMに変更しました。あなたは、ページにそれをリファクタリングする必要があります。

public class A 
{ 
    public string Str { get; set; } 
} 

public class ParentAViewModel : INotifyPropertyChanged 
{ 
    private int _currentIndex; 

    public ParentAViewModel() 
    { 
     Items = new List<A>(); 
    } 

    public string Name { get; set; } 
    public List<A> Items { get; set; } 

    public int CurrentIndex 
    { 
     get 
     { 
      return _currentIndex; 
     } 
     set 
     { 
      _currentIndex = value; 
      OnPropertyChanged(); 
      OnPropertyChanged(nameof(CurrentItem)); 
     } 
    } 

    public string CurrentItem => Items[CurrentIndex].Str; 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

public partial class MainWindow : Window 
{ 
    public ParentAViewModel ObjectParrentA { get; set; } 
    public int SelectedItem { get; set; } 

    public MainWindow() 
    { 
     ObjectParrentA = new ParentAViewModel(); 
     ObjectParrentA.Items.Add(new A() { Str = "1" }); 
     ObjectParrentA.Items.Add(new A() { Str = "2" }); 
     ObjectParrentA.Items.Add(new A() { Str = "3" }); 
     // here is binding of ObjectParrentA by JSON data. 
     SelectedItem = 0; 
     this.DataContext = this; 

     InitializeComponent(); 
    } 

    private void NextItem_Click(object sender, RoutedEventArgs e) 
    { 
     ObjectParrentA.CurrentIndex++; 
    } 
} 

XAMLを:あなたは、観察のプロパティにバインドするとき

<StackPanel> 
    <TextBlock Text="{Binding ObjectParrentA.CurrentItem}" /> 
    <Button Name="NextItem" Click="NextItem_Click" Content="Next Item" /> 
</StackPanel> 

WPFは、その最高の状態で動作します - 方法は注意が必要です。これを容易にするためにViewModelを追加するのは一般的です。そのため、ビューにのみ関連するオブジェクトでモデルクラスを汚染することはありません。

ここでは、バインディングを介してプロパティCurrentItemを見ています。ボタンをクリックすると、CurrentItemがCurrentIndex設定ツールのOnPropertyChanged(nameof(CurrentItem))によって変更されたことがWPFに通知され、ビューが新しい値に再バインドされます。

私はそれが理にかなっていると思います。がんばろう。

+0

ObervableCollection – lindexi

+0

ありがとう、それは非常にうまく動作します:) – DzeryCZ

+0

あなたは大歓迎です:) – KnowHoper