autocompletebox focus in wpf

前端 未结 7 1709
無奈伤痛
無奈伤痛 2020-12-20 15:57

when I try to focus on my \"autocompletetextbox\" I failed I write autocompletetextbox.focus() but the cursor still focus in another what should I do or write t

相关标签:
7条回答
  • 2020-12-20 16:20

    It seems that you have to wait for the auto complete box to load first. Then set focus

    <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();
    
    0 讨论(0)
  • 2020-12-20 16:21

    You will have to override the Focus method to find the template of the Textbox.

    public class FocusableAutoCompleteBox : AutoCompleteBox
    {
        public new void Focus()
        {
            var textbox = Template.FindName("Text", this) as TextBox;
            if (textbox != null) textbox.Focus();
        }
    }
    
    0 讨论(0)
  • 2020-12-20 16:24

    This is my solution for setting focus on AutoCompleteTextBox control Text:

    private void MyPageLoaded(object sender, RoutedEventArgs e) {

    var myPage = (MyControl)sender;
    var autoTextBox = (AutoCompleteTextBox)myPage.FindName("AutoTextBox");
    
    if (autoTextBox != null)
    {
        var innerTextBox = autoTextBox.textBox;
        if (innerTextBox != null)
        {
            innerTextBox.Focus();
    
        }
    }
    

    }

    0 讨论(0)
  • 2020-12-20 16:28

    I experienced the same thing -- it does not work properly in its current form (I expect you're talking about the AutoCompleteBox that comes with the February 2010 release of WPFToolkit).

    I created a subclass:

    public class AutoCompleteFocusableBox : AutoCompleteBox
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var textbox = Template.FindName("Text", this) as TextBox;
            if(textbox != null) textbox.Focus();
        }
    }
    

    This sets focus to the actual TextBox (called "Text") that is part of the default ControlTemplate.

    0 讨论(0)
  • 2020-12-20 16:38

    This is very old question, but I want to share my work-around.

    Keyboard.Focus(autocompletetextbox);
    autocompletetextbox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    

    This works in WPFToolkit v3.5.50211.1 on Visual Studio Express 2015 for Windows Desktop

    0 讨论(0)
  • 2020-12-20 16:46

    This is my solution,

    I found it easier than having inherited class

    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;
    }
    
    0 讨论(0)
提交回复
热议问题