2016-08-15 71 views
0

我在列表中綁定列表<>中的一個項目時遇到問題。XAML在UWP中綁定一個List <>項目

我已匯入JSON文件中的數據,我想經歷在它的陣列,並顯示在文本框。

的數據是這樣的:

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

這裏是C#示例:

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:應顯示

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

在TextBox中開始「 aaa「,點擊Next Item按鈕後應該有」bbb「等...

現在TextBlock是空白的。我認爲在「綁定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。點擊按鈕後,我們通過WPF通知CurrentItem已通過CurrentIndex設置器的OnPropertyChanged(nameof(CurrentItem))更改,然後將視圖重新綁定到新值。

我希望這是有道理的。祝你好運。

+0

ObervableCollection – lindexi

+0

非常感謝你,它工作得很好:) – DzeryCZ

+0

不客氣:) – KnowHoper

相關問題