将ViewCell条目集中在ListView项目选择上 [英] Focus ViewCell Entry on ListView item selection
问题描述
我有一个带有自定义ViewCell的Xamarin.Forms ListView,该ViewCell包含带有Entry和Label的StackLayout.我希望Entry能够在选择ViewCell的任何时候得到关注.
I have a Xamarin.Forms ListView with a custom ViewCell that contains a StackLayout with an Entry and Label. I'd like the Entry to get focus any time that the ViewCell is selected.
ItemSelected
提供选定的项目,但不提供ViewCell.我可以为ViewCell创建一个自定义类,但看不到如何知道它何时被选中.
ItemSelected
provides the item that is selected, but not the ViewCell. I can create a custom class for the ViewCell, but I don't see how it can know when it is selected.
如何将条目集中在ListView项的选择上?
How can I focus the Entry upon ListView item selection?
推荐答案
将条目移动到视图模型中,然后将该条目绑定到ViewCell中的 ContentView
.现在,在 ListView.ItemSelected
事件处理程序中, SelectedItem
包含条目,您可以在其上调用 Focus
.
Move the Entry into the view model, and bind the Entry to a ContentView
in the ViewCell. In the ListView.ItemSelected
event handler, SelectedItem
now contains the Entry, on which you can call Focus
.
XAML看起来像这样:(在此示例中,条目包含一个数量.)
The XAML looks like this: (In this example, the Entry holds a quantity.)
<ViewCell>
<StackLayout Orientation="Horizontal">
<ContentView Content="{Binding QuantityView}" />
<Label ... />
</StackLayout>
</ViewCell>
视图模型如下:
public class MyViewModel {
public string Quantity {get; set;}
public Entry QuantityView { get; } = new Entry {...};
public MyViewModel() {
QuantityView.SetBinding(Entry.TextProperty, nameof(Quantity));
}
}
ItemSelected
事件处理程序如下:
void FocusQuantity(object sender, SelectedItemChangedEventArgs e) {
if (e.SelectedItem == null) return;
((MyViewModel)e.SelectedItem).QuantityView.Focus();
}
这篇关于将ViewCell条目集中在ListView项目选择上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!