C# INotifyPropertyChanged from observablecollection's item

问题描述 投票:0回答:1

简而言之,当我修改可观察集合中项目的属性时,我试图更新我的视图。当我修改其中的添加/删除项目时,可观察的集合句柄,但当我修改其中一个值时不是。

在 ViewModel 中我有这个 Observable 集合:

public ObservableCollection<ListItem> Items
    {
        get { return primaryBin.Items; }
        set { primaryBin.Items = value; OnPropertyChanged(); }
    }

在代码中,我称之为:

                if (Found) 
                {
                    viewModel.IsBusy = true;

                    //Check if there are already some of this product received
                    ListItem origin = viewModel.Items.FirstOrDefault(i => i.EAN == labelText);

                    updateErp = false;
                    SupplierOrderLineForScan p = viewModel.lines.FirstOrDefault(pr => pr.Ean.Trim() == labelText.Trim());
                    u = await Common.GetPickingUser();

                    //Check if there is already some of this product on the trolley
                    ListItem bincontentDirection = viewModel.NotCurrentItems.FirstOrDefault(i => i.EAN == labelText);
                    BinContent bincontentOrigin = viewModel.NotCurrentBinContents.Find(s => s.StockId == p.StockId);


                    if (bincontentDirection == default(ListItem))
                    {
                        bincontentDirection = new ListItem { 
                            Quantity = 0
                            , EAN = origin.EAN, Name = origin.Name};
                        viewModel.ItemsTransfer.Add(bincontentDirection);
                    }
                    bincontentDirection.Quantity++;
                    origin.Quantity--;

                }

但是当我修改“来源”(这是可观察集合中的一项)中的数量时,它不会刷新视图。

我已经尝试着解决这个问题,但是尽管我已经阅读了所有关于它的帖子,但它并没有点击。 Observable 集合处理本机添加/删除,我在 Basic 视图模型中有一些东西可以处理项目中的修改,但似乎没有任何效果。

protected bool SetProperty<T>(ref T backingStore, T value,
        [CallerMemberName]string propertyName = "",
        Action onChanged = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingStore, value))
            return false;

        backingStore = value;
        onChanged?.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var changed = PropertyChanged;
        if (changed == null)
            return;

        changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

编辑:添加 ListItem 的模型

public class BaseModel : INotifyPropertyChanged
{
    protected bool SetProperty<T>(ref T backingStore, T value,
        [CallerMemberName] string propertyName = "",
        Action onChanged = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingStore, value))
            return false;

        backingStore = value;
        onChanged?.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var changed = PropertyChanged;
        if (changed == null)
            return;

        changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}
public class ListItem : BaseModel
{
    //BinConten id
    public string Id { get; set; }
    public bool Block { get; set; }
    public string Name { get; set; }
    public bool IsBlockIconVisible { get; set; }
    public string CodeSupplier { get; set; }
    public bool IsHeader { get; set; }
    public string Entry { get; set; }
    public String ProductID { get; set; }
    public bool showDeleteButton { get; set; }
    public bool showTransferButton { get; set; }
    public int BinID { get; set; }
    public int Piece { get; set; }

    private int _quantity;
    public int Quantity
    {
        get { return _quantity; }
        set { SetProperty(ref _quantity, value); }
    }

    public int BookedQuantity { get; set; }

    public int QuantityDefect { get; set; }

    public Color bgColor { get; set; }

    int _quantityDefect;
    public string EAN { get; set; }

    public string QuantityDisplay
    {
        get { return $"Q: {Quantity}(@{BookedQuantity})"; }
    }

    public string QuantityExpectedDisplay
    {
        get { return $"{Quantity} / {BookedQuantity}"; }
    }

    public string EANReference
    {
        get { return $"{EAN} / {CodeSupplier}"; }
    }

    public Style Style { get; set; }
}
c# android xamarin.forms inotifypropertychanged
1个回答
0
投票

好的,所以我找到了我的问题。我正在正确更新数量,但我的绑定是在未更新的显示值 (QuantityDisplay) 上。

© www.soinside.com 2019 - 2024. All rights reserved.