there is an overload for 'OnKeyDownHandler' matches the delegate 'KeyEventHandler'

1

I have the following method that has the OnKeyDownHandler event and I can only send it two parameters and I want to send it a list as well (FileInformation FilesList)

private void OnKeyDownHandler(object sender, FileInformation FilesList, KeyEventArgs e)
    {
        if (e.Key == Key.Return)
        {
            TextBox tb = ((TextBox)sender);
            new ConnectorCfdiTool.ViewModel.MainToolVM().BuscarCommand(tb.Text, FilesList);
        }
    }

So I have it in the xaml

<TextBox Name="UserInput" LostFocus="UserInput_LostFocus" HorizontalAlignment="Left" Height="23" Margin="565,35,0,0" VerticalAlignment="Top" Width="191" KeyDown="OnKeyDownHandler" />
    
asked by 02.10.2017 в 16:14
source

1 answer

1

The subject of the method KeyDown only accepts 2 parameters that are the Object sender and the event that would be KeyEventArgs . So the delegate must have the same subject of the event but it will throw an error to you.

What you can do is use the Tag property of the control and assign it the FileInformation object and then get it in the execution of the event.

To assign it would be like this:

UserInput.Tag = fileInfoObj;

Then in your event to get the reference of FileInformation you only have to convert the property Tag to FileInformation :

private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Return)
        {
            TextBox tb = ((TextBox)sender);
            FileInformation info = tb.Tag as FileInformation;
            new ConnectorCfdiTool.ViewModel.MainToolVM().BuscarCommand(tb.Text, info);
        }
    }

Or make the object private in the class and get the reference when the method is executed. For example:

public class MainWindow : Window
{
    private FileInformation fileInfo;
    public MainWindow()
    {
      //...
      this.fileInfo = ObtenerFileInfo();
    }

    private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Return)
        {
            TextBox tb = ((TextBox)sender);
            new ConnectorCfdiTool.ViewModel.MainToolVM().BuscarCommand(tb.Text, fileInfo);
        }
    }
}
    
answered by 02.10.2017 в 16:21