Partager via


AutoCompleteBox.TextFilter Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Property TextFilter As AutoCompleteFilterPredicate(Of String)
public AutoCompleteFilterPredicate<string> TextFilter { get; set; }

Property Value

Type: System.Windows.Controls.AutoCompleteFilterPredicate<String>
The custom method that uses the user-entered text to filter items specified by the ItemsSource property in a text-based way for display in the drop-down.

Remarks

The filter mode is automatically set to Custom if you set the TextFilter property.

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

Examples

The following example shows how to set the FilterMode to Custom, and then set the ItemFilter property to a custom text filter method that evaluates the string returned from the ToString method of 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.

Private employees As New List(Of Employee)()
Public Sub New()
    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 TextFilter property to the search method. 
myACB.TextFilter = AddressOf SearchEmployees


...


End Sub


...


Private Function SearchEmployees(ByVal search As String, ByVal value As String) As Boolean
    value = value.ToLower()
    ' Split the string a new line. 
    Dim words As String() = value.Split(System.Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)

    Dim names As String() = words(0).Split(" "c)

    ' Look for a match in the first line; discarding the "employee:" entry. 
    For Each name As String In names
        If name <> "employee:" Then
            If name.StartsWith(search) Then
                Return True
            End If
        End If
    Next
    ' If no match, return false. 
    Return False
End Function


...


Public Class Employee
    Private _LastName As String
    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property
    Private _FirstName As String
    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property
    Private _EmailName As String
    Public Property EmailName() As String
        Get
            Return _EmailName
        End Get
        Set(ByVal value As String)
            _EmailName = value
        End Set
    End Property
    Private _ID As Integer
    Public Property ID() As Integer
        Get
            Return _ID
        End Get
        Set(ByVal value As Integer)
            _ID = value
        End Set
    End Property
    Public Sub New(ByVal empLastName As String, ByVal empFirstName As String, ByVal empEmail As String, ByVal empID As Integer)
        LastName = empLastName
        FirstName = empFirstName
        EmailName = empEmail
        ID = empID
    End Sub
    Public Overloads Overrides Function ToString() As String
        Return ((("Employee: " & FirstName & " ") + LastName + System.Environment.NewLine & "Email: ") + EmailName + System.Environment.NewLine & "ID: ") + ID.ToString()
    End Function
End Class
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 TextFilter property to the search method.
myACB.TextFilter += SearchEmployees;


...


}


...


bool SearchEmployees(string search, string value)
{
    value = value.ToLower();
    // Split the string a new line.
    string[] words = value.Split(System.Environment.NewLine.ToCharArray(),
        StringSplitOptions.RemoveEmptyEntries);

    string[] names = words[0].Split(' ');

    // Look for a match in the first line; discarding the "employee:" entry.
    foreach (string name in names)
    {
        if (name != "employee:")
            if (name.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>

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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