简体   繁体   English

事件未触发

[英]event not being fired

I have made a custom dialog window that inherits ChildWindow 我做了一个自定义对话框窗口,它继承了ChildWindow

public partial class InputWindow : ChildWindow
{
    public InputWindow()
    {
        InitializeComponent();
    }

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("clicked");
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = false;
    }

    private void inputTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
            this.OKButton_Click(this, new RoutedEventArgs());
    }
}

When I press enter in the tetxbox the event OKButton_Click gets fired ( because message box appears). 当我在tetxbox中按下Enter时,事件OKButton_Click被触发(因为出现消息框)。 However, the code (Add Folder) in the event handler below that exists in another class does not get fired! 但是,下面的事件处理程序中存在于另一个类中的代码(添加文件夹)不会被触发! even though the message box appears! 即使出现消息框! Why is this so? 为什么会这样呢? and How can I fix it? 以及如何解决?

InputWindow win = new InputWindow();
win.Title = "Enter New Folder Name";
win.OKButton.Click += (s, e) =>
{
    if (!string.IsNullOrWhiteSpace(win.inputTextBox.Text))
    {
        AddNewFolder(win.inputTextBox.Text);
        win.DialogResult = true;
    }
};
win.Show();

You're just calling OKButton_click directly from your KeyDown event handler. 您只是直接从KeyDown事件处理程序中调用OKButton_click That's not the same as raising the Click event on the OK button itself - it's just a method call. 那是一样的提高Click确定按钮本身的事件-它只是一个方法调用。 So it's no wonder that other event handlers for OKButton.Click aren't being called. 因此,难怪没有调用OKButton.Click其他事件处理程序。

I don't know of any way of manually raising the Click event yourself. 我不知道自己手动引发Click事件的任何方式。 It sounds like really you should have one common method which is called from both the Click event handler and the KeyDown event handler. 听起来确实应该有一个同时从Click事件处理程序和KeyDown事件处理程序中调用的通用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM