我在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
沒有你的代碼(或它的最有意義的部分),這幾乎是不可能幫你 –
是否爲工作您? –