Reputation: 449
I have a ListView
in my project with some elements nested in a Grid
inside the ViewCell
. One of them is a Entry:
<ListView x:Name="NotasListView" ItemsSource="{Binding Avaliacoes}"
HasUnevenRows="True"
IsRefreshing="{Binding Source={x:Reference NotasView}, Path=IsBusy}}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid Padding="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="70"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" x:Name="lblOrdem" Text="{Binding ORDEM}" FontSize="12" Margin="1,5" TextColor="{Binding Cor}"></Label>
<Label Grid.Column="1" x:Name="lblMatricula" Text="{Binding MATRICULA}" FontSize="12" Margin="7,5" TextColor="{Binding Cor}"></Label>
<StackLayout Grid.Column="2">
<Label x:Name="lblNome" Text="{Binding NOME}" FontSize="12" Margin="6,1" TextColor="{Binding Cor}"></Label>
<Label x:Name="lblSituacao" Text="{Binding DescricaoSituacao}" FontSize="12" Margin="6,1" TextColor="{Binding Cor}" IsVisible="{Binding IsVisible}"></Label>
</StackLayout>
<Entry Grid.Column="3" Keyboard="Numeric" x:Name="txtNota" FontAttributes="Bold"
HorizontalOptions="FillAndExpand"
HorizontalTextAlignment="Center"
Margin="7,1" FontSize="14"
Text="{Binding NOTA}"
IsEnabled="{Binding IsEnabled}"
Completed="txtNota_Completed">
<Entry.Behaviors>
<behavior:NotaBehavior x:Name="NotaBehavior" />
</Entry.Behaviors>
</Entry>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
As you can see, the list content is dynamic. Is there some way to change programmatically the focus to the next Entry
on Completed
event?
Upvotes: 1
Views: 1048
Reputation: 41
In the ListView you can bind both the SelectedItemIndex and the SelectedItem in TwoWay mode. So, if the selectedItem is completed you can increase the SelectedItemIndex in your code behind/viewModel.
Upvotes: 0
Reputation: 10938
You could find all the entry in the listview, get the index of the current and then set the focus to the next entry. I use the AutomationId to get the index.
Xaml:
<ListView
x:Name="NotasListView"
HasUnevenRows="True"
ItemsSource="{Binding Avaliacoes}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid Padding="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="170" />
<ColumnDefinition Width="70" />
</Grid.ColumnDefinitions>
<Label
x:Name="lblOrdem"
Grid.Column="0"
Margin="1,5"
FontSize="12"
Text="{Binding ORDEM}"
TextColor="{Binding Cor}" />
<Label
x:Name="lblMatricula"
Grid.Column="1"
Margin="7,5"
FontSize="12"
Text="{Binding MATRICULA}"
TextColor="{Binding Cor}" />
<StackLayout Grid.Column="2">
<Label
x:Name="lblNome"
Margin="6,1"
FontSize="12"
Text="{Binding NOME}"
TextColor="{Binding Cor}" />
<Label
x:Name="lblSituacao"
Margin="6,1"
FontSize="12"
IsVisible="{Binding IsVisible}"
Text="{Binding DescricaoSituacao}"
TextColor="{Binding Cor}" />
</StackLayout>
<Entry
x:Name="txtNota"
Grid.Column="3"
Margin="7,1"
AutomationId="{Binding txtNotaID}"
Completed="txtNota_Completed"
FontAttributes="Bold"
FontSize="14"
HorizontalOptions="FillAndExpand"
HorizontalTextAlignment="Center"
IsEnabled="{Binding IsEnabled}"
Keyboard="Numeric"
Text="{Binding NOTA}">
<!--<Entry.Behaviors>
<behavior:NotaBehavior x:Name="NotaBehavior" />
</Entry.Behaviors>-->
</Entry>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code behind:
public partial class MainPage : ContentPage
{
public ObservableCollection<Info> Avaliacoes { get; set; }
List<Cell> list { get; set; }
public MainPage()
{
InitializeComponent();
//var listView = FindByName<ListView>("NotasListView")
Avaliacoes = new ObservableCollection<Info>()
{
new Info{ Cor=Color.Accent, DescricaoSituacao="DescricaoSituacao1", IsEnabled=true, IsVisible=true, MATRICULA="MATRICULA1", NOME="NOME1", NOTA="NOTA1", ORDEM="NOTA1" ,txtNotaID=1},
new Info{ Cor=Color.AntiqueWhite, DescricaoSituacao="DescricaoSituacao2", IsEnabled=true, IsVisible=true, MATRICULA="MATRICULA2", NOME="NOME2", NOTA="NOTA2", ORDEM="NOTA2",txtNotaID=2},
new Info{ Cor=Color.Red, DescricaoSituacao="DescricaoSituacao3", IsEnabled=true, IsVisible=true, MATRICULA="MATRICULA3", NOME="NOME3", NOTA="NOTA3", ORDEM="NOTA3",txtNotaID=3},
new Info{ Cor=Color.Accent, DescricaoSituacao="DescricaoSituacao4", IsEnabled=true, IsVisible=true, MATRICULA="MATRICULA4", NOME="NOME4", NOTA="NOTA4", ORDEM="NOTA4",txtNotaID=4},
new Info{ Cor=Color.Gray, DescricaoSituacao="DescricaoSituacao5", IsEnabled=true, IsVisible=true, MATRICULA="MATRICULA5", NOME="NOME5", NOTA="NOTA5", ORDEM="NOTA5",txtNotaID=5},
new Info{ Cor=Color.DarkRed, DescricaoSituacao="DescricaoSituacao6", IsEnabled=true, IsVisible=true, MATRICULA="MATRICULA6", NOME="NOME6", NOTA="NOTA6", ORDEM="NOTA6",txtNotaID=6},
new Info{ Cor=Color.GreenYellow, DescricaoSituacao="DescricaoSituacao7", IsEnabled=true, IsVisible=true, MATRICULA="MATRICULA7", NOME="NOME7", NOTA="NOTA7", ORDEM="NOTA7",txtNotaID=7},
new Info{ Cor=Color.Accent, DescricaoSituacao="DescricaoSituacao8", IsEnabled=true, IsVisible=true, MATRICULA="MATRICULA8", NOME="NOME8", NOTA="NOTA8", ORDEM="NOTA8",txtNotaID=8},
};
this.BindingContext = this;
}
private void txtNota_Completed(object sender, EventArgs e)
{
var entry = sender as Entry; // .. and check for null
var list = NotasListView.TemplatedItems.ToList();
var index = Convert.ToInt32(entry.AutomationId)-1;
var nextIndex = (index + 1) >= list.Count ? 0 : index + 1;
foreach (ViewCell item in list)
{
var elements = (item.View as Grid).Children;
}
var next = ((list[nextIndex] as ViewCell).View as Grid).Children.ElementAt(3);
next?.Focus();
}
}
public class Info
{
public string ORDEM { get; set; }
public string MATRICULA { get; set; }
public Color Cor { get; set; }
public string NOME { get; set; }
public string DescricaoSituacao { get; set; }
public bool IsVisible { get; set; }
public bool IsEnabled { get; set; }
public string NOTA { get; set; }
public int txtNotaID { get; set; }
}
}
Upvotes: 1