Export (0) Print
Expand All

AutoCompleteBox.ItemFilter Property

Silverlight

Gets or sets the custom method that uses the user-entered text to filter the items specified by the ItemsSource property for display in the drop-down.

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls.Input (in System.Windows.Controls.Input.dll)

public AutoCompleteFilterPredicate<Object> ItemFilter { get; set; }

Property Value

Type: System.Windows.Controls.AutoCompleteFilterPredicate<Object>
The custom method that uses the user-entered text to filter the items specified by the ItemsSource property. The default is null.

The search mode is automatically set to Custom if you set the ItemFilter property.

Use ItemFilter to provide custom object filtering for items displayed in the drop-down. Alternatively, you should use the TextFilter property to provide custom text filtering.

The following example shows how to set the FilterMode to Custom, and then set the ItemFilter property to a custom filter method for the Employee object. The custom filter returns matches from employees' first or last name. This code example requires a reference to the System.Windows.Controls.Input assembly.


List<Employee> employees = new List<Employee>();
public MainPage()
{
    InitializeComponent();
    // Add some items to the employee list.
    employees.Add(new Employee("Sells", "Chris", "csells", 1234));
    employees.Add(new Employee("Cabatana", "Reina", "rcaba", 5678));
    employees.Add(new Employee("Sprenger", "Christof", "cspreng", 9123));
    employees.Add(new Employee("Brandel", "Jonas", "jbrandel", 4567));
    employees.Add(new Employee("Bye", "Dennis", "dbye", 8912));
    employees.Add(new Employee("Reid", "Miles", "mreid", 3456));

    // Set the item source.
    myACB.ItemsSource = employees;


...


// Set the ItemFilter property to the search method.
myACB.ItemFilter += SearchEmployees;


...


}


...


bool SearchEmployees(string search, object value)
{
    // Cast the value to an Employee.
    Employee emp = value as Employee;
    if (emp != null)
    {
        // Look for a match in the first and last names.
        if (emp.LastName.ToLower().StartsWith(search))
            return true;

        else if (emp.FirstName.ToLower().StartsWith(search))
            return true;
    }
    // If no match, return false.
    return false;
}


...


public class Employee
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string EmailName { get; set; }
    public int ID { get; set; }
    public Employee(string empLastName, string empFirstName, string empEmail, int empID)
    {
        LastName = empLastName;
        FirstName = empFirstName;
        EmailName = empEmail;
        ID = empID;
    }
    public override string ToString()
    {
        return "Employee: " + FirstName + " " + 
            LastName + System.Environment.NewLine + 
            "Email: " + EmailName + System.Environment.NewLine + "ID: " +
            ID.ToString();
    }
}



<StackPanel x:Name="LayoutRoot" Background="LightGray">
     <TextBlock FontWeight="Bold" Text="AutoCompleteBox Custom Filter Example" Margin="5"/>
     <StackPanel  Orientation="Horizontal">
         <TextBlock Text="Employee:" Margin="5" HorizontalAlignment="Left"  VerticalAlignment="Center"/>
         <sdk:AutoCompleteBox Height="75" Width="200" VerticalAlignment="Center" HorizontalAlignment="Right" 
         x:Name="myACB" FilterMode="Custom" ToolTipService.ToolTip="Enter employee name."/>
     </StackPanel>
     </StackPanel>


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2015 Microsoft