[英]Why is the ItemClick event handler returning the previous clicked item?
I have a ListView with a ListView ItemTemplate with a grid. 我有一个带有网格的ListView ItemTemplate的ListView。 I need the information for the row that is clicked and have IsItemClickEnabled=True
on the ListView and a ItemClick
event handler with a debug that shows the id of the row. 我需要单击的行的信息,并在ListView上具有IsItemClickEnabled=True
,并具有带有调试的ItemClick
事件处理程序,该调试器显示该行的ID。
What i cant figure out is why the returned item is always the one that i clicked before, and the first click produces nothing. 我无法弄清楚的是,为什么返回的项目始终是我之前单击过的项目,而第一次单击却什么也没有产生。
Here is the markup for the ListView 这是ListView的标记
<ListView x:Name="ListCustomerView" ItemsSource="{x:Bind CustomersFromDB}" Grid.Row="1" HorizontalAlignment="Center" IsItemClickEnabled="True" ItemClick="ListCustomerView_ItemClick">
<ListView.ItemTemplate>
<DataTemplate>
<Grid IsTapEnabled="False">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="{StaticResource minColumnWidth}" MaxWidth="{StaticResource maxColumnWidth}"></ColumnDefinition>
<ColumnDefinition MinWidth="{StaticResource minColumnWidth}" MaxWidth="{StaticResource maxColumnWidth}"></ColumnDefinition>
<ColumnDefinition MinWidth="{StaticResource minColumnWidth}" MaxWidth="{StaticResource maxColumnWidth}"></ColumnDefinition>
<ColumnDefinition MinWidth="{StaticResource minColumnWidth}" MaxWidth="{StaticResource maxColumnWidth}"></ColumnDefinition>
<ColumnDefinition MinWidth="{StaticResource minColumnWidth}" MaxWidth="{StaticResource maxColumnWidth}"></ColumnDefinition>
<ColumnDefinition MinWidth="{StaticResource minColumnWidth}" MaxWidth="{StaticResource maxColumnWidth}"></ColumnDefinition>
<ColumnDefinition MinWidth="{StaticResource minColumnWidth}" MaxWidth="{StaticResource maxColumnWidth}"></ColumnDefinition>
<ColumnDefinition MinWidth="{StaticResource minColumnWidth}" MaxWidth="{StaticResource maxColumnWidth}"></ColumnDefinition>
<ColumnDefinition MinWidth="{StaticResource minColumnWidth}" MaxWidth="{StaticResource maxColumnWidth}"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding CompanyName}"></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding FirstName}"></TextBlock>
<TextBlock Grid.Column="2" Text="{Binding LastName}"></TextBlock>
<TextBlock Grid.Column="3" Text="{Binding Phone}"></TextBlock>
<TextBlock Grid.Column="4" Text="{Binding Email}"></TextBlock>
<TextBlock Grid.Column="5" Text="{Binding Adress}"></TextBlock>
<TextBlock Grid.Column="6" Text="{Binding PostCode}"></TextBlock>
<TextBlock Grid.Column="7" Text="{Binding PostArea}"></TextBlock>
<TextBlock Grid.Column="8" Text="{Binding Comment}"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Here is the code for the CustomersFromDB ItemsSource 这是CustomersFromDB ItemsSource的代码
public List<Customer> CustomersFromDB { get; set; }
var db = new Database();
CustomersFromDB = db.FetchCustomer();
public List<Customer> FetchCustomer()
{
using (SQLiteConnection _connection = new SQLiteConnection(sqlitePlatform, dbFilePath))
{
List<Customer> customers = new List<Customer>();
var _customers = _connection.Table<Customer>();
if (_customers.Count() > 0)
{
foreach (Customer customer in _customers)
{
customers.Add(customer);
}
}
return customers;
}
}
and the event handler 和事件处理程序
private void ListCustomerView_ItemClick(object sender, ItemClickEventArgs e)
{
// BUG! the selected item is not firing correctly ending up outputting the previous clicked item
Debug.WriteLine((sender as ListView).SelectedItem);
}
The customers are retrieved as seen in the illustration above but when i click a row the id is the previous that is clicked and null if it is the first click. 如上图所示,将检索客户,但是当我单击一行时,id是被单击的前一个ID,如果是第一次单击,则为null。
Why is this wierd behaviour happening? 为什么会发生这种奇怪的行为?
That's because you're using the sender.SelectedItem
which gives you the current selection. 那是因为您正在使用sender.SelectedItem
,它为您提供当前选择。 The new selection, which is what you want, is only made after the click
event. 仅在click
事件之后才进行所需的新选择。 To get the item that you've just clicked, use the e
parameter: 要获取您刚刚单击的项目,请使用e
参数:
private void ListCustomerView_ItemClick(object sender, ItemClickEventArgs e)
{
Debug.WriteLine(e.ClickedItem.ToString());
}
Alternatively, you can use the SelectionChanged
event instead of the ItemClick
event. 或者,您可以使用SelectionChanged
事件而不是ItemClick
事件。 This will give you new selection using the same code like yours: 这将使用与您相同的代码为您提供新的选择:
private void ListCustomerView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine((sender as ListView).SelectedItem.ToString());
}
I haven't tested it, but I'm guessing that the click event happens before the selection event happens. 我尚未对其进行测试,但是我猜测单击事件在选择事件发生之前发生。 Thus, when your click event handler fires, the selecteditem is whatever is "previously" selected. 因此,当您的点击事件处理程序触发时,selecteditem是“先前”选择的内容。 I believe you're looking for the ClickedItem property on the ItemClickedEventArgs 我相信您正在寻找ItemClickedEventArgs上的ClickedItem属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.