Focus AutocompleteBox

11 posts, 1 answers
  1. band
    band avatar
    31 posts
    Member since:
    Jun 2016

    Posted 27 Oct 2016 Link to this post

    Hi, when entering the view, I would like the user to be instantly able to type away at the autocompletebox upon view loaded. I tried Autocompletebox.Focus event but that doesn't quite focus. May I ask how abouts do I focus Autocompletebox?
  2. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    450 posts

    Posted 28 Oct 2016 Link to this post

    Hello band,

    In order to achieve the desired behavior you can handle the view's Loaded event, get ahold of the RadAutoCompleteBox by its Name property or using the ChildrenOfType extension method.

    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }
     
    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        //this.ChildrenOfType<RadAutoCompleteBox>().First().Focus();
        this.autoCompleteBox.Focus();
    }

    Both of the aforementioned approaches work correctly at my end. Could you please let me know if they work for you as well? If that is not the case, please provide more information regarding your exact setup and I will happily assist you further.

    Regards,
    Dilyan Traykov
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
  3. band
    band avatar
    31 posts
    Member since:
    Jun 2016

    Posted 31 Oct 2016 Link to this post

    Hi Dilyan,

    This conflicts with some current code i have at the moment.

    I have a GotFocus event, when the autocompletebox is clicked, it populates autocopletebox's itemsSource.

    I have a RadBusyIndicator that appears while itemsSource is populating, the autocompleteBox loses it's focus when radbusyindicator appears. I've put an await and tried to set the focus again but still no luck.

    private bool doubleFocus = false;
    private async void AutoCompleteBox_OnGotFocus(object sender, RoutedEventArgs e)
    {
        if (!doubleFocus)
        {
            var dataContext = (GeneralInquiryViewModel) DataContext;
            await dataContext.InitializeCollectionAsync();
            doubleFocus = true;
            this.autoCompleteBox.Focus();
        }
             
    }

     

     

  4. Nasko
    Admin
    Nasko avatar
    642 posts

    Posted 02 Nov 2016 Link to this post

    Hi,

    Please, check the following article from our help documentation that provides detailed information how to restore the focus to the desired control after RadBusyIndicator hides:
    http://docs.telerik.com/devtools/wpf/controls/radbusyindicator/how-to/restore-the-focus

    Hope this will help you.

    Regards,
    Nasko
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
  5. band
    band avatar
    31 posts
    Member since:
    Jun 2016

    Posted 02 Nov 2016 in reply to Nasko Link to this post

    Hi, 

    I can't seem to use the first method because it points to element radBusiIndicator but the two objects don't reside on the same user control.

    <telerik:RadBusyIndicator x:Name="RadBusyIndicator"
                              IsBusy="{Binding ShellBusy}"
                              BusyContentTemplate="{StaticResource ProgressSpinner}" 
                              Background="Transparent"
                              BorderBrush="Transparent" KeyboardNavigation.IsTabStop="False" >
        <telerik:RadBusyIndicator.ProgressBarStyle>
            <Style TargetType="telerik:RadProgressBar">
                <Setter Property="Visibility" Value="Collapsed"/>
            </Style>
        </telerik:RadBusyIndicator.ProgressBarStyle>
        <ContentControl x:Name="ActiveItem"
                        HorizontalContentAlignment="Stretch"
                        VerticalContentAlignment="Stretch"                       
                        KeyboardNavigation.IsTabStop="False" />
    </telerik:RadBusyIndicator>

     

     

    So what I tried to do instead was point it to the ancestor  but it produces an error upon loading. Any help would be greatly appreciated. 

     

    <Telerik:RadAutoCompleteBox
                          Framework:FocusHelper.EnsureFocus="{Binding IsBusyIndicationVisible, RelativeSource={RelativeSource FindAncestor, AncestorType=Telerik:RadBusyIndicator}}"

           

  6. Nasko
    Admin
    Nasko avatar
    642 posts

    Posted 04 Nov 2016 Link to this post

    Hello,

    Could you please provide us a sample project that demonstrates your exact scenario? From the provided code-snippet we are not exactly sure how exactly the BusyIndicator and the AutoCompleteBox are arranged in your XAML. Using the sample we will be able to observe your exact scenario on our side and we will try to provide you with a prompt solution.

    We are looking forward to hearing from you.

    Regards,
    Nasko
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
  7. band
    band avatar
    31 posts
    Member since:
    Jun 2016

    Posted 06 Jan Link to this post

    Hi,

    I tried to attach the file but the attachment options only allow the following extensions : jpg, jpeg, gif and png.

    So I've uploaded the file here http://www.filedropper.com/telerikdemo 

     

    I've create a sample solution to illustrate my issue.

    Look forward to hearing back from you. 

  8. Nasko
    Admin
    Nasko avatar
    642 posts

    Posted 09 Jan Link to this post

    Hello,

    We have modified the provided sample with the described in my previous response approach - notice that now the focus is restored to RadAutoCompleteBox when the BusyIndicator hides.

    Hope this helps.

    Regards,
    Nasko
    Telerik by Progress
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  9. band
    band avatar
    31 posts
    Member since:
    Jun 2016

    Posted 09 Jan in reply to Nasko Link to this post

    Hi Nasko,

    Thank you taking the time to help with this situation. It solution you provided works really well, the only thing I was wondering is how to not set the focus to the autocomplete box upon loading the view. It should only trigger the focus on click. 
  10. band
    band avatar
    31 posts
    Member since:
    Jun 2016

    Posted 09 Jan in reply to band Link to this post

    Sorry I should specify alittle more what I mean.

    In my main project I have the busy indicator running to load the view. So since that runs on initial load, it automatically focus the autocomplete box.

    public HomeViewModel(IEventAggregator events)
    {
        Events = events;
        BusyTrigger();
    }

  11. Answer
    Nasko
    Admin
    Nasko avatar
    642 posts

    Posted 10 Jan Link to this post

    Hi,

    You could achieve the desired by handling the PreviewMouseLeftButtonDown event of the AutoCompleteBox. Using a bool property you could indicate if the event has been triggered and only it it has been to call the Focus method - thus it will be focused only on click.

    The project has been modified with the described approach.

    Hope this helps.

    Regards,
    Nasko
    Telerik by Progress
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top