ListView Xamarin在添加项目时没有绑定

时间:2017-01-24 09:02:16

标签: xamarin xamarin.forms

我正在使用XF应用程序中的ListView控件。我的XAML代码看起来像这样。

    <ListView  ItemsSource="{Binding RechargeList}" HasUnevenRows="True" VerticalOptions="FillAndExpand">
  <ListView.ItemTemplate>
    <DataTemplate>
      <TextCell Text="{Binding Path=SelectedParkingID}" TextColor="Red" />
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

虽然我的代码背后看起来像

    private ObservableCollection<Recharge> _RechargeList = new ObservableCollection<Recharge>();
    public ObservableCollection<Recharge> RechargeList
    {
        get
        {
            return _RechargeList;
        }
        set
        {
            SetProperty(ref _RechargeList, value);
        }

    }           

我在DelegateCommand Event

中添加了Items
RechargeList.Add(new Recharge() { SelectedParkingIDParkingID = ParkingID, RechargeAmount = double.Parse(RechargeAmount), BalanceAmount = 10 });

但是,Listview无法刷新。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

看起来你有一个错字

<TextCell Text="{Binding Path=SelectedParkingID}" TextColor="Red" />

应该是

<TextCell Text="{Binding Path=SelectedParkingIDParkingID }" TextColor="Red" />

基于您的模型的样子。如果您尝试绑定到不存在的属性,则会轻轻失败。因此,您要添加一个项目,但TextCell没有呈现,因为它没有内容。

答案 1 :(得分:-1)

请尝试在您的班级中实施INotifyPropertyChanged界面。

public class Data : INotifyPropertyChanged
{
 // boiler-plate
  public event PropertyChangedEventHandler PropertyChanged;
  protected virtual void OnPropertyChanged(string propertyName)
  {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null) handler(this, new   PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, string propertyName)
{
    if (EqualityComparer<T>.Default.Equals(field, value)) return false;
    field = value;
    OnPropertyChanged(propertyName);
    return true;
}

   // props
  private string name;
  public string Name
  {
    get { return name; }
    set { SetField(ref name, value, "Name"); }
  }
}

每个属性都是这样的:

private string name;
public string Name
{
    get { return name; }
    set { SetField(ref name, value, "Name"); }
}