我正在使用ListView,在每个单元格中,我有一个标签和右侧的条目。当我点击输入框时,键盘会显示,但如果我点击该行而不点击输入框键盘则不会。如何在选择行时使条目聚焦?
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Sample7.StartPage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Scanner" Icon="camera.png">
</ToolbarItem>
<ToolbarItem Text="Notes" Icon="notes.png" >
</ToolbarItem>
</ContentPage.ToolbarItems>
<ScrollView>
<StackLayout Padding="20,20,20,0">
<Picker Title="Location" HorizontalOptions="FillAndExpand" Items="NuseedDataManager.getWarehouses()"/>
<StackLayout Orientation="Horizontal" Padding="0,20,0,20">
<Label Text="Show All" HorizontalOptions="FillAndExpand" />
<Switch IsToggled="true" HeightRequest="50" WidthRequest="250"/>
</StackLayout>
<ListView x:Name="SkuListView" RowHeight="60">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" BackgroundColor="{Binding BackgroundColor}">
<Label Text="TITLE\nawesome1awesome1awesome1awesome1awesome1awesome1awesome1awesome1awesome1awesome1awesome1awesome1" HorizontalOptions="FillAndExpand" FontSize="12"/>
<Entry Text="{Binding CountText}" WidthRequest="70" Keyboard="Numeric" IsFocused="{Binding IsEntryFocused}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout Padding="0,20,0,20">
<Button Text="Verify" />
</StackLayout>
</StackLayout>
</ScrollView>
</ContentPage>
namespace Sample7
{
public partial class StartPage : ContentPage
{
ObservableCollection<Cell> skus ;
public StartPage ()
{
InitializeComponent ();
skus = new ObservableCollection<Cell>();
for (int i = 0; i < 30; i++) {
if (i < 10) {
skus.Add(new Cell(){
BackgroundColor = Color.Aqua,
CountText = "1"
});
} else {
skus.Add (new Cell () {
BackgroundColor = Color.Red,
CountText = "1"
});
}
}
SkuListView.ItemsSource = skus;
SkuListView.HeightRequest = SkuListView.RowHeight* skus.Count ()+40;
SkuListView.ItemSelected += (sender, e) => {
Cell c = (Cell)e.SelectedItem;
c.IsEntryFocused = true;
c.BackgroundColor = Color.Olive;
c.CountText = "45";
};
}
}
}
答案 0 :(得分:0)
为条目创建新行为,并将其BindingContext设置为ListView实例。如果所选行发生更改(ItemSelected事件),请将所选项目与当前行的项目(entry.BindingContext)进行比较。如果匹配,请关注该条目。
<Entry Text="{Binding CountText}" WidthRequest="70" Keyboard="Numeric">
<Entry.Behaviors>
<local:FocusEntryFromSelectedRowBehavior BindingContext="{x:Reference SkuListView}" />
</Entry.Behaviors>
</Entry>
public class FocusEntryFromSelectedRowBehavior : Behavior<Entry>
{
public Entry AssociatedEntry{ get; private set; }
protected override void OnAttachedTo(Entry entry)
{
base.OnAttachedTo(entry);
AssociatedEntry = entry;
((ListView)BindingContext).ItemSelected += MyView_ItemSelected;
}
private void MyView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (AssociatedEntry.BindingContext == e.SelectedItem)
{
AssociatedEntry.Focus();
}
}
protected override void OnDetachingFrom(Entry entry)
{
((ListView)BindingContext).ItemSelected -= MyView_ItemSelected;
base.OnDetachingFrom(entry);
}
}
答案 1 :(得分:-1)
您需要调用Entry的Focus()方法。