在我的类(class)中,我为 ListBoxItem 编写了双击事件。单击列表框的条目时,它应该只返回该特定条目。但就我而言,虽然我单击了单个条目,但所有条目都被返回并且出现“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"/>
请您参考如下方法:
ListBox 隐式地将其项目包装到 ListBoxItem 中。尝试将 sender 转换为 ListBoxItem 然后获取其 Content 属性
