Xamarin Forms Listview将焦点设置为所选行中的条目

本文关键字:Listview Forms 焦点 设置 Xamarin | 更新日期: 2023-09-27 18:29:26

我正在使用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";
			};
		}
	}
}

Xamarin Forms Listview将焦点设置为所选行中的条目

为条目创建一个新行为,并将其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);
    }
}

您需要调用Entry的Focus()方法。