在我的課中,我爲ListBoxItem寫了雙擊事件。當單擊列表框的條目時,它應該只返回該特定條目。但在我的情況下,雖然我點擊了單個條目,但所有條目都返回併發生「InvalidCastException」。所以,我應該如何改變才能獲得單一入場。發生類型'System.InvalidCastException'的第一次機會異常
這裏是雙擊事件代碼:
private void ListBoxItem_DoubleClick(object sender, RoutedEventArgs e)
{
//Submit clicked Entry
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)sender;
if (!entryToPost.isSynced)
{
//Check if something is selected in selectedProjectItem For that item
if (entryToPost.ProjectNameBinding == "Select Project")
MessageBox.Show("Please Select a Project for the Entry");
else
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
}
else
{
//Already synced.. Make a noise or something
MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}
}
In xml:
<ListBox x:Name="listBox1" ItemsSource="{Binding}" Margin="0,131,0,59" ItemTemplateSelector="{StaticResource templateSelector}" ListBoxItem.MouseDoubleClick="ListBoxItem_DoubleClick"/>
什麼行會拋出無效的轉換異常?你確定發件人是Harvest_TimeSheetEntry嗎?根據你的解釋,我不認爲它是。 –
您可以使用ListBox.SelectedItem獲取雙擊事件中的點擊項目 –
此行顯示異常 - Harvest_TimeSheetEntry entryToPost =(Harvest_TimeSheetEntry)sender; – Dinesh