且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将对象的List属性绑定到ListView

更新时间:2022-06-03 03:21:09

就目前而言,我至少需要纠正三件事:

As for now I see at least three things you need to correct:

  • 在您的MainPage中实现INotifyPropertyChanged,例如:

  • implement INotifyPropertyChanged in your MainPage, for example like this:

public partial class MainPage: Page, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaiseProperty(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

  • 您绑定的对象应该是公共属性:

  • your objects which you bind to, should be public properties:

    private ObservableCollection<RootObject> weatherObjects;
    
    public ObservableCollection<RootObject> WeatherObjects
    {
        get { return weatherObjects; }
        set { weatherObjects= value; RaiseProperty(nameof(WeatherObjects)); }
    }
    

  • 根据您的代码,您可能还需要从默认的 OneTime 更改绑定的模式:

    <ListView Grid.Row="2" ItemsSource="{x:Bind WeatherObjects, Mode="OneWay"}">
    

  • 您无需像使用 x:Bind 那样设置 DataContext .

    You don't need to set the DataContext as you are using x:Bind.