我在XAML中有一个文本框(当然,一些文本框),这些行为不当。当我将注意力集中到文本框(有或没有输入任何内容)时,经过一段时间后,特定文本框会自动失去焦点。对于某些文本框,它发生得很快,而对于一些文本框却很慢,但总是发生。它在过去3天里杀了我,但找不到任何东西。它只是一个普通的文本框。如果任何人有任何想法或背后的可能性,请提及它。C#WPF XAML自动文本框失去焦点而打字
回答
我面临这个问题太时,我曾与复杂的GUI有很多表,主信息等坦率地说,我也没搞清楚什么是这个问题的原因,但有时只专注迷路了在打字过程中。 我解决了这个问题,此行为:在XAML
public class TextBoxBehaviors
{
public static bool GetEnforceFocus(DependencyObject obj)
{
return (bool)obj.GetValue(EnforceFocusProperty);
}
public static void SetEnforceFocus(DependencyObject obj, bool value)
{
obj.SetValue(EnforceFocusProperty, value);
}
// Using a DependencyProperty as the backing store for EnforceFocus. This enables animation, styling, binding, etc...
public static readonly DependencyProperty EnforceFocusProperty =
DependencyProperty.RegisterAttached("EnforceFocus", typeof(bool), typeof(TextBoxBehaviors), new PropertyMetadata(false,
(o, e) =>
{
bool newValue = (bool)e.NewValue;
if (!newValue) return;
TextBox tb = o as TextBox;
if (tb == null)
{
MessageBox.Show("Target object should be typeof TextBox only. Execution has been seased", "TextBoxBehaviors warning",
MessageBoxButton.OK, MessageBoxImage.Warning);
}
tb.TextChanged += OnTextChanged;
}));
private static void OnTextChanged(object o, TextChangedEventArgs e)
{
TextBox tb = o as TextBox;
tb.Focus();
/* You have to place your caret at the end of your text manually, because each focus repalce your caret at the beging of text.*/
tb.CaretIndex = tb.Text.Length;
}
}
用法:
<TextBox x:Name="txtPresenter"
behaviors:TextBoxBehaviors.EnforceFocus="True"
Text="{Binding Path=MyPath, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Center" />
对于漫长的等待回应感到抱歉,但它对我无效。谢谢你的信息先生。 B.你对此有任何其他想法吗? –
尝试发布代码段。 –
其实我所做的就是,我创建TextBoxBehaviors类如上准确,在文本框,我用行为:TextBoxBehaviors.EnforceFocus =“真”。此外,还有一件事,当我打开文本框所在的对话框时,首先我将焦点放在该文本框上并等待5秒钟,然后文本框焦点丢失。我认为你的情况只适用于textboxchanged的情况,对吗?也许我错过了一些东西。 –
- 1. 阻止WPF文本框失去焦点
- 2. XAML/WPF焦点文本块
- 3. 文本框失去焦点与jQuery自动完成选择
- 4. wpf键盘焦点:菜单打开后失去焦点?
- 5. jQuery对话框失去焦点滚动
- 6. 关于文本框焦点的边框颜色。 C#Xaml Windows Phone8.1
- 7. WPF - 防止ListView失去焦点
- 8. WPF刷新TreeView时失去焦点
- 9. UIAutomation c#文本框消失与鼠标单击或失去焦点
- 10. 如何在WPF中失去焦点时触发文本框命令?
- 11. java - 失去焦点
- 12. 层失去焦点
- 13. UIGestureRecognizer“失去焦点”?
- 14. TextBox失去焦点
- 15. AJAX替代物失去焦点文本框
- 16. asp.net文本框失去焦点的方法?
- 17. 对话框失去焦点在Windows XP
- 18. WPF:把焦点放回文本框递归调用失去焦点事件。如何解决?
- 19. WPF组合框:如何知道项目容器(何时打开)失去焦点
- 20. 将焦点设置文本框在WPF
- 21. wpf文本框焦点问题
- 22. 在WPF文本框中设置焦点
- 23. 如何在Emacs上自动保存失去焦点的文件
- 24. 如何让Vim在失去焦点时自动保存文件?
- 25. 滚动时编辑文本失去焦点
- 26. 输入框由于广泛的目标而失去焦点
- 27. 的文本框焦点打开WPF弹出
- 28. 文本框在光标选择移动到结尾后失去焦点
- 29. C#RDP ActiveX控件失去焦点
- 30. 在C++中失去焦点和GotFocus
没有你的代码(或它的最有意义的部分),这几乎是不可能帮你 –
是否为工作您? –