I can't select an element in RadAutoCompletebox

1 Answer 69 Views
Accessibility AutoCompleteBox AutoSuggestBox
Tadeáš
Top achievements
Rank 1
Tadeáš asked on 29 Jul 2024, 12:02 PM | edited on 29 Jul 2024, 12:56 PM

Hello,

 

I need some advice about RadAutoCompleteBox.

When I click on RadAutoCompleteBox I want to see all possible results and then I will have a option to choose whether I want to select from them or start searching by typing.  Searching for text and then selecting works fine, but unfortunately searching with nothing in it doesn't work. When I click on RadAutoCompleteBox all the results are displayed, but I can't select anything from them until I type something. The hover selection works fine, I just can't select anything if RadAutoCompleteBox.SearchText is empty. Could you help me how to solve this? Thank you very much.

I am sending part of my the source code and example (WpfApp1.zip) where it also not working.


... 
 <controls:RadAutoCompleteBoxFilter x:Key="RadAutoCompleteBoxFilter" />
...
     <telerik:RadAutoCompleteBox Grid.Row="0"
                      x:Name="CaseAutoCompleteBox"
                      WatermarkContent="{x:Static rs:ResourceText.Start_Typing}"
                      ItemsSource="{Binding Suggestions}"
                      SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                      TextSearchMode="Contains"
                      TextSearchPath="Name"
                      DisplayMemberPath="Name"
                      AutoCompleteMode="SuggestAppend"
                      DropDownItemTemplate="{StaticResource CasesSearchAutoComplete}"
                      SelectionMode="Single"
                      SelectionChanged="CaseAutoCompleteBoxSelectionChanged"
                      PreviewMouseLeftButtonDown="OnPreviewMouseLeftButtonDown"
                      GotFocus="CaseAutoCompleteBox_OnGotFocus"
                      FilteringBehavior="{StaticResource RadAutoCompleteBoxFilter}"
                    
                      />



 public partial class AddMediaToCase 
 {
     public AddMediaToCase()
     {
         InitializeComponent();
         RadWindowInteropHelper.SetShowInTaskbar(this, true);
     }

     private void CaseAutoCompleteBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
     {
         var autoCompleteBox = sender as RadAutoCompleteBox;
         CaseInformationStackPanel.Visibility = autoCompleteBox.SelectedItem != null ? Visibility.Visible : Visibility.Collapsed;
     }

     private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
     {

         if (string.IsNullOrWhiteSpace(CaseAutoCompleteBox.SearchText))
         {
             CaseAutoCompleteBox.Populate(" ");
             
         }


     }

     private void CaseAutoCompleteBox_OnGotFocus(object sender, RoutedEventArgs e)
     {
         if (string.IsNullOrWhiteSpace(CaseAutoCompleteBox.SearchText))
         {
             
             CaseAutoCompleteBox.Populate(" ");
         }

     }
 }


  public class RadAutoCompleteBoxFilter : FilteringBehavior
  {
      public override IEnumerable<object> FindMatchingItems(string searchText, IList items,
          IEnumerable<object> escapedItems, string textSearchPath,
          TextSearchMode textSearchMode)
      {
          var result =
              base.FindMatchingItems(searchText, items, escapedItems, textSearchPath, textSearchMode) as
                  IEnumerable<object>;

          if (string.IsNullOrEmpty(searchText) || !result.Any())
          {
              return ((IEnumerable<object>)items).Where(x => !escapedItems.Contains(x));
          }

          return result;
      }
  }


 

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 30 Jul 2024, 10:33 AM

Hello Tadeáš,

Thank you for the project adn the description.

The issue occurs because the GotFocus event is raised also when you click on an item in the drop down. This triggers the Populate method again, which opens the drop down again.

To resolve this, instead of GotFocus, you can use the MouseLeftButtonDown event. For example:

 acb.AddHandler(RadAutoCompleteBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Acb_MouseLeftButtonDown), true);
 
 private void Acb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 {
     var clickedElement = (FrameworkElement)e.OriginalSource;
     if (clickedElement.ParentOfType<RadWatermarkTextBox>() != null)
     {
         acb.Populate(string.Empty);
     }
 }

Can you try this and let me know if it helps?

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tadeáš
Top achievements
Rank 1
commented on 31 Jul 2024, 07:40 AM | edited

 

Hello,

thank you very much for your reply.

I have tried the suggested solution, but unfortunately the error is still the same. When you click on
AutoCompleteBox I get all the results, but when I click on some
 one of them doesn't happen until the first character is typed.
I am sending the code that I have tried.

 

 


  public partial class AddMediaToCase 
  {
      public AddMediaToCase()
      {
          InitializeComponent();
          RadWindowInteropHelper.SetShowInTaskbar(this, true);
          CaseAutoCompleteBox.AddHandler(RadAutoCompleteBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(CaseAutoCompleteBox_MouseLeftButtonDown), true);
      }

      private void CaseAutoCompleteBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          var autoCompleteBox = sender as RadAutoCompleteBox;
          CaseInformationStackPanel.Visibility = autoCompleteBox.SelectedItem != null ? Visibility.Visible : Visibility.Collapsed;
      }

      private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {

          if (string.IsNullOrWhiteSpace(CaseAutoCompleteBox.SearchText))
          {
              CaseAutoCompleteBox.Populate(string.Empty);
              
          }


      }

      private void CaseAutoCompleteBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          var clickedElement = (FrameworkElement)e.OriginalSource;
          if (clickedElement.ParentOfType<RadWatermarkTextBox>() != null)
          {
              CaseAutoCompleteBox.Populate(string.Empty);
          }
          else
          {

          }
      }
   
      private void CaseAutoCompleteBox_KeyDown(object sender, KeyEventArgs e)
      {
          if (e.Key == Key.Space && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
          {
              CaseAutoCompleteBox.Populate(string.Empty);
              e.Handled = true;
          }
      }


  }



     <telerik:RadAutoCompleteBox Grid.Row="0"
                      x:Name="CaseAutoCompleteBox"
                      WatermarkContent="{x:Static rs:ResourceText.Start_Typing}"
                      ItemsSource="{Binding Suggestions}"
                      SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                      TextSearchMode="Contains"
                      TextSearchPath="Name"
                      DisplayMemberPath="Name"
                      AutoCompleteMode="SuggestAppend"
                      DropDownItemTemplate="{StaticResource CasesSearchAutoComplete}"
                      SelectionMode="Single"
                      SelectionChanged="CaseAutoCompleteBoxSelectionChanged"
                      PreviewMouseLeftButtonDown="OnPreviewMouseLeftButtonDown"
                      FilteringBehavior="{StaticResource RadAutoCompleteBoxFilter}"
                      KeyDown="CaseAutoCompleteBox_KeyDown"
                    
                      />

Thank you very much.

Martin Ivanov
Telerik team
commented on 01 Aug 2024, 12:16 PM

This doesn't work because of the code in the OnPreviewMouseLeftButtonDown event handler, which basically does the same as the suggested CaseAutoCompleteBox_MouseLeftButtonDown, but it doesn't have the if-condition that checks for the clicked element type.

To resolve this, remove the OnPreviewMouseLeftButtonDown, or add the if-block from the CaseAutoCompleteBox_MouseLeftButtonDown event handler.

Tags
Accessibility AutoCompleteBox AutoSuggestBox
Asked by
Tadeáš
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or