按钮不能单击Wpf

时间:2013-08-22 11:21:13

标签: c# wpf button mouseclick-event

在我的wpf应用中,我的按钮无法单击。我有用于更改ListBox项的dataTemplate的Edit按钮。但是,我需要点击两次按钮进行编辑。对于右键单击事件,我需要单击两次。双击事件工作正常。为什么会这样?哪个可能是问题?

右键单击事件和编辑按钮的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";
            }
        }

    }

1 个答案:

答案 0 :(得分:3)

尝试处理PreviewMouseDownPreviewMouseLeftButtonDown事件。有时冒泡Routed Events会被控件内部使用。有关详情,请查看MSDN上的UIElement.PreviewKeyDown EventUIElement.PreviewMouseDown Event页。