0
votes

I've a simple window which includes 2 fields :

<TextBox Grid.Column="1" HorizontalAlignment="Left" Height="21" Margin="10,25,0,0" Grid.Row="1" Name="textbox_login" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="227" KeyDown="textbox_login_KeyDown"/>
            <PasswordBox Grid.Column="1" HorizontalAlignment="Left" Height="22" Margin="10,25,0,0" Grid.Row="2" Name="passwordBox_mdp" VerticalAlignment="Top" Width="227" KeyDown="passwordBox_mdp_KeyDown"/>

When I press the tab key on my keyboard I can switch to the next field but once I've reached the last field if I press the key again I have an exception on "MyApplication.Run" :

System.InvalidOperationException : Targettype button does not correspond to Control Element

Stacktrace : System.Windows.Style.CheckTargetType(Object element)

This error happens on all my windows/UC even if there isn't any field (just textblocks)

I don't know at all how to solve this.

1
What do you do in those KeyDown handlers? - 500 - Internal Server Error
I only have 2 KeyDown handler in this window : private void textbox_login_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { valider(); } } private void passwordBox_mdp_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { valider(); } } - user2088807
Does the error disappear if you comment out those calls to valider()? - 500 - Internal Server Error
No, the error also appears on windows where I don't have any KeyDown handlers. - user2088807

1 Answers

0
votes

I was getting a similar error in a modal dialog. To debug, I added a PreviewLostKeyboardFocus event handler to the dialog. Examining the KeyboardFocusChangedEventArgs NewFocus property told me which control the focus change was hitting a problem on.

For me it occurred when tabbing from a close Button to a TabItem in the dialog. To work around the issue I set KeyboardNavigation.IsTabStop="false" on the TabItem so that hitting the tab key would ignore the TabItem when changing focus. My TabItem looked like this:

<TabItem Name="MyTab" Header="Test Header" VerticalContentAlignment="Stretch"
 VerticalAlignment="Stretch" Height="Auto" KeyboardNavigation.IsTabStop="False">

There's probably a better solution, but this prevents the exception.