简体   繁体   中英

WPF Text Changed Event Not Firing on 'S'

I have a WPF application that is a user control hosted in another company's application.

The textboxes I have created will accept all characters and numbers except for capital 'S'. They are MVVM bound to string properties that work on any other character than 'S'. The text changed events fire for every other character as well. The on key down and up will fire off for the "S" character though. The text box does not display the character.

I read somewhere that sometimes a conflict happens with hosted user controls in which it will not accept certain inputs, but I am unable to set the ElementHost property needed to address this as I do not have programmatic access to the host window in this case. The control is hosted by another User Control which is hosted under the third party application I am adding an application too.

I am dumbfounded as every other key works. I checked this on another machine to find the same problem, so it is not hardware dependent.

I suspect that you've subscribed for the TextChanged event. If that is the case just subscribe for PreviewKeyDown , apply your filter and set e.Handled to true to sink it.

private void PreviewKeyDownFilter(object sender, KeyEventArgs e)
{
    if (e.Key == Key.S && Keyboard.IsKeyDown(Key.LeftShift))
        e.Handled = true;
}

I figured out a not so clean way to handle it. I am not happy with the solution as I would like to understand the root cause, but this works functionally.

 private void IOTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.S && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)))
        {
            TextBox tb = sender as TextBox;
            tb.Text = tb.Text + "S";
            tb.CaretIndex = tb.Text.Length;
            e.Handled = true;
        }

    }

If anyone else has any better idea...

Try to create a separate WPF app that does the same: dummy app that hosts a dummy control that hosts your control. If everything works OK, there is a 99% chance their app has some accelerator shortcut or another kind of logic that eats that "S" char.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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