【问题标题】:Focus to entry of collectionview at specific index关注特定索引处的collectionview条目
【发布时间】:2021-03-21 20:04:58
【问题描述】:

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

在将项目插入列表之前,我会进行 linq 搜索,如果列表中不存在该项目,则将其插入。 到目前为止,一切都很好。 但是当 linq 搜索返回一行时,我想以某种方式将注意力集中在条目上,这样我就可以更改它的文本(它作为一个数量字段)。

我的代码尽可能简单:

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, 0);

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

【问题讨论】:

    标签: c# xamarin xamarin.forms


    【解决方案1】:

    我没有看到你的班级的 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;
    

    【讨论】:

    • 我有 OnPropertyChanged,我忘了把它粘贴到问题上。你是说修改数量。我想要的是专注于入口,所以用户可以插入他想要的数量
    • ScrollTo 方法将滚动到所需的行,但不会聚焦 Entry
    • 我正在考虑在 Iteline 类中创建一个 Entry 属性。然后尝试类似 IteLines[linkqIndex].EntryQty.Focus() 的方法。我的问题是我不知道如何为EntrQty 设置值。 CreateItemTemplate() 必须以某种方式更新 Itemlines 才能设置每行的 EntryQty
    • stackoverflow.com/questions/48811089/…这个链接帮你绑定Entry但是调用Focus() onitemselected
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2017-07-27
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多