This is a migrated thread and some comments may be shown as answers.

radcombobox with IsEditable Property search with two keyword like using FilteringBehavior in radautocompletebox

5 Answers 144 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
ابو
Top achievements
Rank 1
ابو asked on 09 Nov 2016, 06:07 PM

hi my friends

I wanna to use radcombobox in editable mode to search with two key words

by employee id and employee name

for each key word I wanna to show employee name in radcombobox

example:

table from database

id             name

----           ---------

10            jack

20            ali

30            dany

search by employee id like picture 1.

search by employee name like picture 2.

 

5 Answers, 1 is accepted

Sort by
0
Kalin
Telerik team
answered on 11 Nov 2016, 08:59 AM
Hello,

RadComboBox provides support for implementing a custom Filtering Behavior. For more details, please check the following article from our help documentation:
http://docs.telerik.com/devtools/wpf/controls/radcombobox/features/filteringbehavior

Hope this helps.

Regards,
Kalin
Telerik by Progress
Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
0
ابو
Top achievements
Rank 1
answered on 15 Nov 2016, 09:01 AM
OK , but how I can use it in edit mode to search with employee id or employee name
0
Kalin
Telerik team
answered on 17 Nov 2016, 09:57 AM
Hi,

This can be achieved by overriding the FindMatchingIndexes method and implement a logic that filters the items depending on the input. Check the following snippet:


public class CustomFilteringBehavior : ComboBoxFilteringBehavior
{
    public override List<int> FindMatchingIndexes(string text)
    {
        int id = -1;
        // if the input is number select the items with matching id
        if (int.TryParse(text, out id))
        {
            return this.ComboBox.Items.OfType<DataItem>().Where(i => i.Id == id).Select(i => this.ComboBox.Items.IndexOf(i)).ToList();
        }
 
        // if the input is string select the items with matching name
        return this.ComboBox.Items.OfType<DataItem>().Where(i => i.Name.ToLowerInvariant().Contains(text.ToLowerInvariant())).Select(i => this.ComboBox.Items.IndexOf(i)).ToList();
    }
}

Hope this helps.

Regards,
Kalin
Telerik by Progress
Telerik UI for WPF is ready for Visual Studio 2017 RC! Learn more.
0
ابو
Top achievements
Rank 1
answered on 27 Nov 2016, 05:59 PM

thanks

but I have error in Id (i.Id)

Where(i => i.Id == id)

error: 'DataItem' dose not contain a definition for 'Id' .......

0
Don
Top achievements
Rank 1
answered on 05 Jun 2020, 05:21 PM

I am having the same problem:

ENVIRONMENT: Silverlight 5.0 , Telerik.Windows.Controls = 2016.2.503.1050

1) I have created a custom Filter Behavior

public class RadComboBoxCustomFilterBehavior : ComboBoxFilteringBehavior 
    {
        public override List<int> FindMatchingIndexes(string text)
        {
            List<int> itemsFound = new List<int>();
            foreach (var item in this.ComboBox.Items)
            {
                var props = item.GetType().GetProperties();
                foreach (PropertyInfo prop in props)
                {
                    if (prop.PropertyType.IsPrimitive || prop.PropertyType == typeof(String))
                    {
                        object propValue = prop.GetValue(item, null);
                        string stringValue = propValue != null ? propValue.ToString() : String.Empty;
                        if (stringValue.ToUpper().Contains(text.ToUpper()))
                        {
                            itemsFound.Add(this.ComboBox.Items.IndexOf(item));
                        }
                    }
                }
            }
            return itemsFound;
        }
    }

2) I created a Model:

public class Claim : ViewModelBase
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; RaisePropertyChanged(() => Id); }
        }

        private string _desc;
        public string Description
        {
            get { return _desc; }
            set { _desc = value; RaisePropertyChanged(() => Description); }
        }
    }

 

3) I created the View Model:

 public class MainViewModel : ViewModelBase
    {

        private IEnumerable<Claim> _claims;
        public IEnumerable<Claim> Claims
        {
            get { return _claims; }
            set { _claims = value; RaisePropertyChanged(() => Claims); }
        }

        private Claim _selectedClaim;
        public Claim SelectedClaim
        {
            get { return _selectedClaim; }
            set { _selectedClaim = value; RaisePropertyChanged(() => SelectedClaim); }
        }


        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}


            Claims = new List<Claim>() { 
                new Claim{ Id = 1001, Description = "The Cat chased the Dog"},
                new Claim{ Id = 1002, Description = "Fish eats cats"},
                new Claim{ Id = 1003, Description = "Cow sat on the dog"},
                new Claim{ Id = 1004, Description = "People love cats, dogs and cows"},
                new Claim{ Id = 1022, Description = "people are people"}
            };
        }
    }    

 

4) I created the VIEW (short version):

<telerik:RadComboBox 
            HorizontalAlignment="Center" 
            VerticalAlignment="Top" 
            Width="200" Margin="15" 
            IsEditable="True"  
            OpenDropDownOnFocus="True" 
            IsFilteringEnabled="True" 
            SelectedItem="{Binding SelectedClaim, Mode=TwoWay}"
            ItemsSource="{Binding Claims}" >

            <telerik:RadComboBox.FilteringBehavior>
                <Behaviors:RadComboBoxCustomFilterBehavior />
            </telerik:RadComboBox.FilteringBehavior>

            <telerik:RadComboBox.ItemTemplate>
                <DataTemplate DataType="model:Claim">
                    <TextBlock>
                        <Run Text="{Binding Id}" />
                        <Run Text=": " />
                        <Run Text="{Binding Description}" />
                    </TextBlock>
                </DataTemplate>
            </telerik:RadComboBox.ItemTemplate>
        </telerik:RadComboBox>

 

You can see the Filtering happening correctly, except the SELECTED ITEM loses the ItemTemplate formating on the TEXT.

TEXT = "ComboBoxFilterDemo.Model.Claim" which is the OBJECT toString()... 

See Attached output.

Tags
ComboBox
Asked by
ابو
Top achievements
Rank 1
Answers by
Kalin
Telerik team
ابو
Top achievements
Rank 1
Don
Top achievements
Rank 1
Share this question
or