Ritesh
Ritesh

Reputation: 362

Selection of item in ListBox based on text entered in TextBox

I was preparing a sample code in which text in ListBox is automatically selected as user makes text entry in TextBox. So far I have achieved the result, but there is no case insensitive matching. Here is code.

XAML

<StackPanel>
    <TextBox Name="txt" />
    <ListBox ItemsSource="{Binding Employees}" DisplayMemberPath="Name" SelectedValuePath="Name" 
             Height="100" SelectedValue="{Binding Text, ElementName=txt}" SelectedItem="{Binding SelectedEmployee}"/>
    <Button Content="OK" Command="{Binding SaveCommand}" />
</StackPanel>

I am binding this XAML with following ViewModel.

ViewModel

public class CheckViewModel : ViewModel.ViewModelBase
{
    IList<Employee> employees;

    Employee _selectedEmployee;

    public CheckViewModel()
    {
        employees = new List<Employee>() { 
            new Employee(1, "Morgan"), 
            new Employee(2, "Ashwin"), 
            new Employee(3, "Shekhar"),
            new Employee(5, "Jack"),
            new Employee(5, "Jill")
        };
    }

    public IList<Employee> Employees
    {
        get
        { return employees; }
    }

    public Employee SelectedEmployee
    {
        get
        { return _selectedEmployee; }
        set
        {
            if (_selectedEmployee != value)
            {
                _selectedEmployee = value;
                this.OnPropertyChanged("SelectedEmployee");
            }
        }
    }

    ICommand _saveCommand;
    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand == null)
            {
                _saveCommand = new ViewModel.RelayCommand((p) =>
                {
                    if(this._selectedEmployee != null)
                        MessageBox.Show(this._selectedEmployee.Name);
                    else
                        MessageBox.Show("None Selected");
                },
                (p) =>
                {
                    return this._selectedEmployee != null;
                });
            }
            return _saveCommand;
        }
    }
}

public class Employee
{
    public Employee(int id, string name)
    {
        this.Name = name;
        this.Id = id;
    }
    public string Name { get; set; }
    public int Id { get; set; }
}

So when I type "Jack" in TextBox, the selection duly occurs. But when I change the case - "jack" - the selection does not occur and SelectedEmployee becomes null. Certainly, the comparison is case sensitive, but how can I change this behaviour?

Could you please guide me how to make the comparison case insensitive?

Upvotes: 0

Views: 773

Answers (2)

paparazzo
paparazzo

Reputation: 45096

If you use a HashSet then you can set the comparer. It will also prevent you from entering duplicate values.

HashSet<string> xxx = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);

Not the question but to compare two strings case insensitive use

String.Compare Method (String, String, Boolean)

Upvotes: 0

syned
syned

Reputation: 2321

You can bind your textbox to property in ViewModel as follows:

    <TextBox Name="txt" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"/>

ViewModel:

    public string SearchText
    {
        get { return _searchText; }
        set
        {
            _searchText = value;
            // provide your own search through your collection and set SelectedEmployee
        }
    }

Upvotes: 2

Related Questions