在 Completed 事件上关注 ListView 中的下一个条目
Focus on next entry in ListView on Completed event
我的项目中有一个 ListView
,其中一些元素嵌套在 ViewCell
中的 Grid
中。其中之一是条目:
<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>
如您所见,列表内容是动态的。有什么方法可以通过编程将焦点更改为 Completed
事件中的下一个 Entry
?
您可以找到列表视图中的所有条目,获取当前条目的索引,然后将焦点设置到下一个条目。我使用 AutomationId 来获取索引。
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>
后面的代码:
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; }
}
}
在 ListView 中,您可以在 TwoWay 模式下绑定 SelectedItemIndex 和 SelectedItem。因此,如果 selectedItem 已完成,您可以在代码中增加 SelectedItemIndex behind/viewModel.
我的项目中有一个 ListView
,其中一些元素嵌套在 ViewCell
中的 Grid
中。其中之一是条目:
<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>
如您所见,列表内容是动态的。有什么方法可以通过编程将焦点更改为 Completed
事件中的下一个 Entry
?
您可以找到列表视图中的所有条目,获取当前条目的索引,然后将焦点设置到下一个条目。我使用 AutomationId 来获取索引。
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>
后面的代码:
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; }
}
}
在 ListView 中,您可以在 TwoWay 模式下绑定 SelectedItemIndex 和 SelectedItem。因此,如果 selectedItem 已完成,您可以在代码中增加 SelectedItemIndex behind/viewModel.