Compartilhar via


AutoCompleteBox.PopulateComplete Method

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

Notifies the AutoCompleteBox that the ItemsSource property has been set and the data can be filtered to provide possible matches in the drop-down.

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

Syntax

'Declaration
Public Sub PopulateComplete
public void PopulateComplete()

Remarks

Call this method when you are providing custom population of the drop-down portion of the AutoCompleteBox, to signal the control that you are done with the population process. Typically, you use PopulateComplete when the population process is a long-running process and you want to cancel built-in filtering of the ItemsSource items. In this case, you can handle the Populating event and set PopulatingEventArgs.Cancel to true. When the long-running process has completed you call PopulateComplete to indicate the drop-down is populated.

Examples

The following code example demonstrates how to handle the Populating event and populate the AutoCompleteBox manually. This code example sets the PopulatingEventArgs.Cancel property to true to stop auto-population and then calls PopulateComplete when the manual population operation is complete. This code example requires a reference to the System.Windows.Controls.Input assembly.

Run this sample

    ' Handle the Populating event, but cancel it. Make the call to the web service. 
    Private Sub AutoCompleteBox_Populating(ByVal sender As Object, ByVal e As PopulatingEventArgs)
        e.Cancel = True
        CallToWebService()
    End Sub

    ' Call the topic service at the live search site. 
    Private wc As WebClient
    Private Sub CallToWebService()
        wc = New WebClient()
        wc.DownloadStringAsync(New Uri("http://api.search.live.net/qson.aspx?query=" & _
            myACB.SearchText & "&sources=news"))
        AddHandler wc.DownloadStringCompleted, AddressOf wc_DownloadStringCompleted
    End Sub

    Private Sub wc_DownloadStringCompleted(ByVal sender As Object, _
            ByVal e As DownloadStringCompletedEventArgs)
        If e.[Error] IsNot Nothing Then
            Exit Sub
        End If

        Dim data As New List(Of String)()
        Try
            Dim jso As JsonObject = TryCast(DirectCast(JsonObject.Parse(e.Result),  _
                JsonObject)("SearchSuggestion"), JsonObject)
            Dim originalSearchString As String = jso("Query")
            If originalSearchString = myACB.SearchText Then
                For Each suggestion As JsonObject In DirectCast(jso("Section"), JsonArray)
                    data.Add(suggestion.Values.First())
                Next

                ' Diplay the AutoCompleteBox drop down with any suggestions 
                myACB.ItemsSource = data
                myACB.PopulateComplete()
            End If
        Catch
        End Try
    End Sub
End Class
    // Handle the Populating event, but cancel it. Make the call to the web service.
    private void AutoCompleteBox_Populating(object sender, PopulatingEventArgs e)
    {
        e.Cancel = true;
        CallToWebService();
    }

    // Call the topic service at the live search site.
    WebClient wc;
    private void CallToWebService()
    {
        wc = new WebClient();
        wc.DownloadStringAsync(new Uri
            ("http://api.search.live.net/qson.aspx?query=" + myACB.SearchText +
            "&sources=news"));
        wc.DownloadStringCompleted +=
            new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
    }

    void wc_DownloadStringCompleted(object sender,
        DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            return;
        }

        List<string> data = new List<string>();
        try
        {
            JsonObject jso = ((JsonObject)JsonObject.Parse(e.Result))
                ["SearchSuggestion"] as JsonObject;
            string originalSearchString = jso["Query"];
            if (originalSearchString == myACB.SearchText)
            {
                foreach (JsonObject suggestion in (JsonArray)jso["Section"])
                {
                    data.Add(suggestion.Values.First());
                }

                // Diplay the AutoCompleteBox drop down with any suggestions
                myACB.ItemsSource = data;
                myACB.PopulateComplete();
            }
        }
        catch { }
    }
}
   <StackPanel x:Name="LayoutRoot" Background="White" Orientation="Horizontal">
    <TextBlock Text="News Topic:" HorizontalAlignment="Left"  VerticalAlignment="Center"/>
    <sdk:AutoCompleteBox VerticalAlignment="Center" HorizontalAlignment="Right" Height="30" 
        Width="100" MinimumPopulateDelay="200" MinimumPrefixLength="3" x:Name="myACB" Populating="AutoCompleteBox_Populating" />
</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.