當我嘗試專注於我的「自動完成文本框」時,我失敗了我寫了autocompletetextbox.focus()
但是,光標仍然集中在另一個我應該做什麼或寫入來啓用寫入或聚焦?autocompletebox focus in wpf
回答
我經歷過同樣的事情 - 它在當前形式下無法正常工作(我期望您談論的是WPFToolkit 2010年2月發行版隨附的AutoCompleteBox)。
我創建了一個子類:
public class AutoCompleteFocusableBox : AutoCompleteBox
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var textbox = Template.FindName("Text", this) as TextBox;
if(textbox != null) textbox.Focus();
}
}
這將焦點設置到實際TextBox
(稱爲「文本」),這是默認ControlTemplate
的一部分。
看來,你必須等待自動完成框加載第一。然後設置集中
<sdk:AutoCompleteBox
x:Name="_employeesAutoCompleteBox"
ItemsSource="{Binding Path=Employees}"
SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}"
ValueMemberPath="DisplayName" >
</sdk:AutoCompleteBox>
_employeesAutoCompleteBox.Loaded +=
(sender, e) => ((AutoCompleteBox)sender).Focus();
你將不得不重寫對焦方法來查找文本框的模板。
public class FocusableAutoCompleteBox : AutoCompleteBox
{
public new void Focus()
{
var textbox = Template.FindName("Text", this) as TextBox;
if (textbox != null) textbox.Focus();
}
}
'using System.Windows.Controls;'too – mbdavis 2013-12-31 10:57:52
這是我上AutoCompleteTextBox控制文本設置焦點溶液:
私人無效MyPageLoaded(對象發件人,RoutedEventArgs E) {
var myPage = (MyControl)sender;
var autoTextBox = (AutoCompleteTextBox)myPage.FindName("AutoTextBox");
if (autoTextBox != null)
{
var innerTextBox = autoTextBox.textBox;
if (innerTextBox != null)
{
innerTextBox.Focus();
}
}
}
這是非常老問題,但我想分享我的解決方法。
Keyboard.Focus(autocompletetextbox);
autocompletetextbox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
這個作品在WPFToolkit v3.5.50211.1
上Visual Studio Express 2015 for Windows Desktop
簡單,適用於我。謝謝! – landsteven 2017-01-05 20:52:56
這是我的解決方案,
我發現比有繼承類
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
var textBox = FindVisualChild<TextBox>(CodedCommentBox);
textBox.Focus();
}
private TChildItem FindVisualChild<TChildItem>(DependencyObject obj) where TChildItem : DependencyObject
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
var item = child as TChildItem;
if (item != null)
{
return item;
}
var childOfChild = FindVisualChild<TChildItem>(child);
if (childOfChild != null)
{
return childOfChild;
}
}
return null;
}
謝謝,它的工作 – Harmi 2017-11-24 11:26:02
- 1. WPF AutoCompleteBox
- 2. Edittext focus in first listview item
- 3. textfield focus in flash not working online
- 4. Focus-TextField in material-ui v1
- 5. WPF工具包autocompletebox的ItemTemplate
- 6. C#:Tooltip on Focus on WPF TextBox
- 7. AutoCompleteBox的Dropdownlist項目不顯示在WPF
- 8. 如何隱藏WPF AutoCompleteBox下拉框
- 9. 動態設置wpf工具包Autocompletebox Itemsource
- 10. wpf - autocompletebox不會在mousedown上更新
- 11. Treeview in viewbox wpf
- 12. Silverlight AutoCompleteBox
- 13. WPF Casting in Binding Path
- 14. wpf style basedon in code
- 15. WPF工具包AutoCompleteBox不刷新其數據
- 16. Silverlight AutoCompleteBox SelectedValue(?)
- 17. AutocompleteBox導航
- 18. Silverlight AutoCompleteBox樣式?
- 19. Silverlight 5 + AutoCompleteBox = Bug
- 20. Silverlight中的AutoCompleteBox
- 21. UITextField focus
- 22. WPF/Silverlight AutoCompleteBox能夠將新值添加到列表中
- 23. WPF AutoCompleteBox - 如何限制它只從建議列表中選擇?
- 24. Expression Blend 4 - AutoCompleteBox不適用於WPF應用程序
- 25. WPF AutoCompleteBox風格的DataTemplate綁定ValueMemberPath內容
- 26. WPF Dockpanel寬度Stretch in xaml
- 27. Textbox Enter Key event not in WPF
- 28. WPF ControlTemplate樣式GradientStop in Trigger
- 29. UserControl Focus Issue - Focus()有時會返回false
- 30. AutoCompleteBox是邪惡的
我應該在哪裏把這個類更容易我在有自動完成框的班上試過,但不起作用? – kartal 2010-08-31 16:03:36
在你的XAML中,你使用這個而不是'AutoCompleteBox'。因此,如果您在命名空間XYZ中擁有此類,則可以使用xyz等別名將該命名空間導入到XAML文件的根目錄中,然後使用 –
Jay
2010-08-31 16:16:50
嘗試,但混合給我一個錯誤AutoCompleteFocusableBox不支持wpf! – kartal 2010-08-31 17:09:48