简体   繁体   English

WPF文本更改事件未在'S'上触发

[英]WPF Text Changed Event Not Firing on 'S'

I have a WPF application that is a user control hosted in another company's application. 我有一个WPF应用程序,它是在另一个公司的应用程序中托管的用户控件。

The textboxes I have created will accept all characters and numbers except for capital 'S'. 我创建的文本框将接受除大写'S'之外的所有字符和数字。 They are MVVM bound to string properties that work on any other character than 'S'. 它们是MVVM绑定到字符串属性,可以处理除“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. 然后,按键向下和向上将触发“S”字符。 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. 我在某处看到有时托管用户控件发生冲突,它不接受某些输入,但我无法设置解决此问题所需的ElementHost属性,因为在这种情况下我没有对主机窗口的编程访问。 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. 我怀疑你已经订阅了TextChanged事件。 If that is the case just subscribe for PreviewKeyDown , apply your filter and set e.Handled to true to sink it. 如果是这种情况,只需订阅PreviewKeyDown ,请应用您的过滤器并将e.Handled设置为true以将其下沉。

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. 尝试创建一个单独的WPF应用程序,它执行相同的操作:虚拟应用程序,它承载一个托管控件的虚拟控件。 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. 如果一切正常,那么他们的应用程序有99%的机会有一些加速器快捷方式或另一种吃“S”字符的逻辑。

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

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