XAML绑定一个List<>项目在UWP

本文关键字:项目 UWP List 一个 绑定 XAML | 更新日期: 2023-09-27 18:02:41

从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" />

在文本框的开头应该显示"aaa",在点击Next Item按钮之后应该显示"bbb",等等…

现在TextBlock是空白的。我认为"Binding objectparent . items [SelectedItem]"一定有问题。,但是我没有太多绑定的经验。

你有什么提示,如何实现这一点?谢谢你。

XAML绑定一个List<>项目在UWP

我稍微改变了一下,使它更接近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。当按钮被点击时,我们通过CurrentIndex设置器的OnPropertyChanged(nameof(CurrentItem))通知WPF currentiitem已经改变了,然后将视图重新绑定到新值。

我希望这有意义。好运。