C# 聚焦到特定索引处collectionview的条目
我正在尝试创建一个订购解决方案。 我有一个绑定到列表的collectionView。 collectionview的datatemplate是在运行时创建的。 在datatemplate中,我有一个条目。 我打电话到一个网络服务,我得到一个项目 在将该项插入列表之前,我进行linq搜索,如果该项在列表中不存在,则将其插入其中。 到现在为止,一直都还不错。 但是当linq搜索返回一行时,我希望以某种方式将焦点放在条目上,这样我就可以更改它的文本(它作为一个数量字段) 我的代码尽可能简单:C# 聚焦到特定索引处collectionview的条目,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我正在尝试创建一个订购解决方案。 我有一个绑定到列表的collectionView。 collectionview的datatemplate是在运行时创建的。 在datatemplate中,我有一个条目。 我打电话到一个网络服务,我得到一个项目 在将该项插入列表之前,我进行linq搜索,如果该项在列表中不存在,则将其插入其中。 到现在为止,一直都还不错。 但是当linq搜索返回一行时,我希望以某种方式将焦点放在条目上,这样我就可以更改它的文本(它作为一个数量字段) 我的代码尽可能简单: publ
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;
});
}
公共类IteLinesViewModel:INotifyPropertyChanged
{
公共可观测集合{get;set;}
}
公共类IteLine:ExtendedBindableObject,INotifyPropertyChanged
{
私有字符串\u名称;
公共字符串名
{
get=>\u Name;
set=>SetProperty(参考名称、值);
}
私人双_数量;
公共双数量
{
获取=>\u数量;
set=>SetProperty(参考数量、值);
}
}
CollectionView4Lines.ItemTemplate=CreateItemTemplate();
私有数据模板CreateItemTemplate()
{
返回新的数据模板(()=>
{
Grid OuterGrid=new Grid(){Margin=0,Padding=0};
添加(新的ColumnDefinition{Width=GridLength.Star});
添加(新的ColumnDefinition{Width=65});
Label MainDisplayLabel=new Label(){TextType=TextType.Html,FontSize=18};
MainDisplayLabel.SetBinding(Label.TextProperty,“名称”);
OuterGrid.Children.Add(MainDisplayLabel,0,0);
条目qtyEntry=新条目();
qtyEntry.SetBinding(Entry.TextProperty,“数量”)
OuterGrid.Children.Add(qtyEntry,1,0);
返回外部网格;
});
}
我没有看到类的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;
我把OnProperty更改了,我忘了把它贴在问题上。你说的是修改数量。我想要的是关注条目,这样用户可以插入他想要的数量。请尝试ScrollTo方法ScrollTo方法将滚动到所需的行,但不会关注条目。我想在Iteline类中创建条目属性。然后尝试类似于IteLines[linkqIndex].EntryQty.Focus()的方法。我的问题是我不知道如何设置EntrQty的值。CreateItemTemplate()必须以某种方式更新Itelines,以便设置每行的EntryQuantity。此链接帮助您绑定条目,但调用Focus()onitemselected