微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

专注于特定索引处的 collectionview 条目

如何解决专注于特定索引处的 collectionview 条目

我正在尝试创建一个订购解决方案。 我有一个绑定到列表的 collectionView。 collectionview 的数据模板是在运行时创建的。 在数据模板中,我有一个条目。 我打电话给一个网络服务,我得到了一个项目。

在我将 Item 插入列表中之前,我会进行 linq 搜索,如果该 Item 不存在于列表中,则将其插入其中。 到现在为止还挺好。 但是当 linq 搜索返回一行时,我想以某种方式将焦点放在条目上,这样我就可以更改它的文本(作为数量字段)。

enter image description here

我的代码尽可能简单:

public class IteLinesviewmodel : INotifyPropertyChanged
{
    public ObservableCollection<IteLine> IteLines { get; set; }
}


public class IteLine : ExtendedBindableObject,INotifyPropertyChanged
{
     private string _Name;
     public string Name
     {
         get => _Name;
         set => SetProperty(ref _Name,value);
     }
     
     private double _qty;
     public double Qty
     {
         get => _qty;
         set => SetProperty(ref _qty,value);
     }
}

CollectionView4Lines.ItemTemplate = CreateItemTemplate();

private DataTemplate CreateItemTemplate()
{
    return new DataTemplate(() =>
    {
        Grid OuterGrid = new Grid() { Margin = 0,Padding = 0 };
        OuterGrid.ColumnDeFinitions.Add(new ColumnDeFinition { Width = GridLength.Star });
        OuterGrid.ColumnDeFinitions.Add(new ColumnDeFinition { Width = 65 });
        
        Label MaindisplayLabel = new Label() { TextType = TextType.Html,FontSize = 18 };
        MaindisplayLabel.SetBinding(Label.TextProperty,"Name");
        OuterGrid.Children.Add(MaindisplayLabel,0);

        Entry qtyEntry = new Entry();
        qtyEntry.SetBinding(Entry.TextProperty,"Qty")
        OuterGrid.Children.Add(qtyEntry,1,0);
        
        return OuterGrid;
    });
}

解决方法

我没有看到您的类的 INotifyPropertyChanged 实现

public class IteLine : INotifyPropertyChanged
{
    private double _qty;
    public string Qty
   {
         get {return _qty;}
         set 
         {
             _qty= value;
             OnPropertyChanged("Qty");
         }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this,new PropertyChangedEventArgs(propertyName));
    }
}

现在,如果您找到要更改的索引。你可以这样做

IteLines[linkqIndex].Qty = 5;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。