RadComboBox Filtering trough Textblock inside the Itemtemplate of the Combobox

1 Answer 115 Views
ComboBox
Kai
Top achievements
Rank 1
Kai asked on 19 Aug 2021, 10:25 AM

Hello,

I want to be able to filter (and also see what I am filtering) when the IsEditable Property of the RadComboBox is false

Is there a way to set a Textblock in the Itemtemplate (like the ClearAll Button) of the RadComboBox to filter through the Items?

 

Regards,

Kai

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 24 Aug 2021, 09:02 AM

Hello Kai,

I assume that by TextBlock you mean TextBox and that you want a single TextBox at the top of the dropdown of the RadComboBox. I'm clarifying this as placing the TextBox in the ItemTemplate of the control will add a separate TextBox for each item which does not seem desirable.

With this said, I've prepared a small sample project which I believe achieves your requirement. For the purpose, I've added a TextBox in the NonEditableComboBox template of the control and handle its TextChanged event. In it, I use the FilteringBehavior of the control to get the proper item to select based on the inputted text.

For this to work as expected, as normally the FilteringBehavior is only used when the IsEditable property is True, I'm calling the EnsureFilteringBehavior method of the control via reflection.

Please have a look and let me know if a similar approach would work for you. Alternatively, you can extract this in an attached behavior and add the TextBox via code instead of modifying the control template.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Kai
Top achievements
Rank 1
commented on 24 Aug 2021, 01:24 PM | edited

Hello, Dilyan thanks for your help.

You helped me a lot for the first step.

Instead of Selecting the SelectedItem of the ComboBox through the TextBox,

I would like to filter the ItemSource so that the user can select the item he wants himself. So in general the same behavior like if you set isFilteredEnabled to true. Just instead of using the TextBox in the ComboBox I would like to use a TextBox in the DropDown

Dilyan Traykov
Telerik team
commented on 26 Aug 2021, 01:05 PM

Hello Kai,

I've updated the sample project to demonstrate how you can achieve a similar functionality to the IsFilteredEnabled property by modifying the Visibility of the items of the combo box if they do not pass the filtering criteria.

        public void UpdateItemsVisibility(RadComboBox comboBox, List<int> matchIndices)
        {
            ItemCollection items = comboBox.Items;
            var noMatches = !matchIndices.Any();

            for (int i = 0; i < items.Count; i++)
            {
                FrameworkElement element = comboBox.ItemContainerGenerator.ContainerFromItem(items[i]) as FrameworkElement;
                if (element != null)
                {
                    bool isMatch = noMatches || matchIndices.Contains(i);

                    var visibility = isMatch ? Visibility.Visible : Visibility.Collapsed;

                    element.Visibility = visibility;
                }
            }
        }

Please note, however, that this approach will not work if the control is virtualized.

Nonetheless, I hope you find this helpful. Please let me know if you're now able to achieve the desired result.

Tags
ComboBox
Asked by
Kai
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or