在我的 wpf 应用程序中,我的按钮在单击时不起作用。我有用于更改 ListBox 项目的 dataTemplate 的编辑按钮。但是,我需要单击两次按钮才能进行编辑。同样对于右键单击事件,我需要单击两次。双击事件工作正常。为什么会发生这种情况?问题可能出在哪里?
右键单击事件和编辑按钮的 C# 代码:
private void listBox1_MouseRightClick(object sender, MouseButtonEventArgs e)
{
Harvest_TimeSheetEntry entryToDelete = (Harvest_TimeSheetEntry)listBox1.SelectedItem;
if(entryToDelete!=null)
{
MessageBoxResult Result = System.Windows.MessageBox.Show("Are you sure?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo);
if (Result == MessageBoxResult.Yes)
{
Globals._globalController.harvestManager.deleteHarvestEntry(entryToDelete);
listBox1.Items.Remove(listBox1.SelectedItem);
}
else
{
System.Windows.MessageBox.Show("Delete operation Terminated");
}
}
}
private void EditButton_Click(object sender, RoutedEventArgs e)
{
if (listBox1.ItemTemplate == (DataTemplate)this.FindResource("DefaultDataTemplate"))
{
listBox1.ItemTemplate = (DataTemplate)this.FindResource("EditableDataTemplate");
EditButton.Content = "Done Editing";
}
else
{
foreach (Harvest_TimeSheetEntry item in listBox1.Items)
{
if (!item.isSynced)
{
ValidateEntry(item);
Globals._globalController.harvestManager.updateHarvestEntry(item);
System.Windows.MessageBox.Show("Entry Updated");
}
listBox1.ItemTemplate = (DataTemplate)this.FindResource("DefaultDataTemplate");
EditButton.Content = "Edit";
}
}
}