j riv
j riv

Reputation: 3699

Why do I get KeyEventArgs from XAML but RoutedEventArgs from C# when I refer to the exact same KeyDown event?

This must be something simple but I can't seem to google it easily.

e.g. <TextBox Name="blah" KeyDown="TextboxKEYDOWN">

And now C# can use an events function with KeyEventArgs available

but if I do in C# blah.KeyDown += (s, r) => TextboxKEYDOWN(s, e);

then TextboxKEYDOWN only gets RoutedEventArgs and complains that it can't cast KeyEventArgs to it.

Why is that and can it be used as normal from C# only?

PS. I have a feeling this is something too simple.

Upvotes: 2

Views: 783

Answers (1)

Clemens
Clemens

Reputation: 128013

That is because in

blah.KeyDown += (s, r) => TextboxKEYDOWN(s, e);

r is apparently not identical to e (no idea though what e is in your code sample).

You should however be able to write it as

blah.KeyDown += TextboxKEYDOWN;

where the handler method should look like this

private void TextboxKEYDOWN(object sender, KeyEventArgs e)
{
    ...
}

Upvotes: 1

Related Questions