AutoCompleteBox.SelectionChanged Event


Occurs when the selected item in the drop-down portion of the AutoCompleteBox has changed.

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

Public Event SelectionChanged As SelectionChangedEventHandler
<sdk:AutoCompleteBox SelectionChanged="eventhandler"/>

For more information about handling events, see Events Overview for Silverlight.

The following example shows how to use the ValueMemberBinding property in combination with a data template to display a data-bound date. There is a value converter associated with the binding that formats the date in a custom format. In addition, the SelectionChanged event is handled, and the details of the selected item are shown in two text block controls. This code example requires a reference to the System.Windows.Controls.Input assembly.

Run this sample

Imports System.Windows.Data

Partial Public Class MainPage
    Inherits UserControl
    Private MyMusic As New List(Of Recording)()
    Public Sub New()

        ' Add items to the collection. 
        MyMusic.Add(New Recording("Chris Sells", "Chris Sells Live", New DateTime(2008, 3, 5)))
        MyMusic.Add(New Recording("Chris Sells", "Chris Sells", New DateTime(2004, 4, 6)))
        MyMusic.Add(New Recording("Luka Abrus", "The Road to Redmond", New DateTime(2007, 8, 3)))
        MyMusic.Add(New Recording("Luka Abrus", "Luka", New DateTime(2005, 12, 8)))
        MyMusic.Add(New Recording("Jim Hance", "The Best of Jim Hance", New DateTime(2007, 2, 6)))

        ' Set the data context for the AutoCompleteBox. 
        myACB.DataContext = MyMusic
        AddHandler myACB.SelectionChanged, AddressOf myACB_SelectionChanged
    End Sub

    ' Handle the SelectionChanged event-setting the data context for the stack panel 
    ' that contains the details to the selected item. 
    Private Sub myACB_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        RecordingDetails.DataContext = e.AddedItems(0)
        Dim acb As AutoCompleteBox = TryCast(sender, AutoCompleteBox)
    End Sub
End Class

    ' Simple business object. 
    Public Class Recording
        Public Sub New()
        End Sub
        Public Sub New(ByVal artistName As String, ByVal cdName As String, ByVal release As DateTime)
            Artist = artistName
            Name = cdName
            ReleaseDate = release
        End Sub
        Private _Artist As String
        Public Property Artist() As String
                Return _Artist
            End Get
            Set(ByVal value As String)
                _Artist = value
            End Set
        End Property
        Private _Name As String
        Public Property Name() As String
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
        Private _ReleaseDate As DateTime
        Public Property ReleaseDate() As DateTime
                Return _ReleaseDate
            End Get
            Set(ByVal value As DateTime)
                _ReleaseDate = value
            End Set
        End Property
    End Class

    Public Class ObjectFormatter
        Implements IValueConverter
        ' This converts the object to the string to display. 
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, _
                            ByVal parameter As Object, _
                            ByVal culture As System.Globalization.CultureInfo) As Object _
                            Implements IValueConverter.Convert

        ' Retrieve the format string and use it to format the value. 
        Dim formatString As String = TryCast(parameter, String)
        If Not String.IsNullOrEmpty(formatString) Then
            Return String.Format(culture, formatString, value)
        End If

        ' If the format string is null or empty, simply call ToString() 
        ' on the value. 
        Return value.ToString()
    End Function

        ' No need to implement converting back on a one-way binding 
    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, _
                                ByVal parameter As Object, _
                                ByVal culture As System.Globalization.CultureInfo) As Object _
                                Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
    End Class

<!-- NOTE: 
  By convention, the sdk prefix indicates a URI-based XAML namespace declaration 
  for Silverlight SDK client libraries. This namespace declaration is valid for 
  Silverlight 4 only. In Silverlight 3, you must use individual XAML namespace 
  declarations for each CLR assembly and namespace combination outside the scope 
  of the default Silverlight XAML namespace. For more information, see the help 
  topic "Prefixes and Mappings for Silverlight Libraries". 
<UserControl xmlns:sdk="" x:Class="ACBValueMemberBinding.MainPage" 
    Width="400" Height="413">
    <StackPanel x:Name="LayoutRoot" Background="White" >
    <StackPanel Orientation="Horizontal">
        <local:ObjectFormatter x:Key="FormatConverter" />
        <TextBlock Margin="5" Text="Enter a date:" TextWrapping="Wrap" />
            <sdk:AutoCompleteBox VerticalAlignment="Top" Margin="5" Width="170" Height="30" x:Name="myACB" 
                ValueMemberBinding="{Binding Path=ReleaseDate, Converter={StaticResource FormatConverter}, 
                ConverterParameter=\{0:d\}}" >
                    <TextBlock Text="{Binding Path=ReleaseDate, Converter={StaticResource FormatConverter}, 
                    ConverterParameter=\{0:d\}}" />
        <Rectangle Fill="DarkGray" Margin="5" Height="5" />
        <StackPanel x:Name="RecordingDetails" Margin="5" Background="Beige">
            <TextBlock FontWeight="Bold" Text="{Binding Path=Artist}" x:Name="artistTextBox" />
            <TextBlock FontStyle="Italic" Text="{Binding Path=Name}" />


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