Condividi tramite


AutoCompleteBox.ValueMemberPath Property

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

Gets or sets the property path that is used to get the value for display in the text box portion of the AutoCompleteBox control, and to filter items 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 ValueMemberPath As String
public string ValueMemberPath { get; set; }
<sdk:AutoCompleteBox ValueMemberPath="path"/>

XAML Values

  • path
    A property path. See Remarks.

Property Value

Type: System.String
The property path that is used to get values for display in the text box portion of the AutoCompleteBox control, and to filter items for display in the drop-down.

Remarks

The AutoCompleteBox uses the ValueMemberPath property to determine where to retrieve values from for comparison to user-entered text. The control displays the filtered values in the drop-down portion of the control. When an item is selected, the ValueMemberPath determines which property value is displayed in the text box.

If you set ValueMemberPath to something other than a string, an object-to-string conversion is attempted by calling to ToString method of the specified type. If ToString does not yield the desired results, and you do not have access to the type to override the ToString method, you should use the ValueMemberBinding property, specifying a Converter for the binding.

To customize the items displayed in the drop-down portion of the AutoCompleteBox, you should specify an ItemTemplate in addition to setting the ValueMemberPath property.

Examples

The following example shows how to use the ValueMemberPath and FilterMode properties to populate the drop-down portion of the AutoCompleteBox control.

Run this sample

Imports System.Collections

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

        ' 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", "Sells Tells", _
            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("Luka Abrus", "Love and Luka ", _
            New DateTime(2005, 11, 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
    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
        Get
            Return _Artist
        End Get
        Set(ByVal value As String)
            _Artist = value
        End Set
    End Property
    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
    Private _ReleaseDate As DateTime
    Public Property ReleaseDate() As DateTime
        Get
            Return _ReleaseDate
        End Get
        Set(ByVal value As DateTime)
            _ReleaseDate = value
        End Set
    End Property
End Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections;

namespace ACBValueMemberPath
{
    public partial class MainPage : UserControl
    {
        List<Recording> MyMusic = new List<Recording>();
        public MainPage()
        {
            InitializeComponent();
            // 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", "Sells Tells",
               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("Luka Abrus",
               "Love and 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;
        }
    }

    // Simple business object.
    public class Recording
    {
        public Recording() { }
        public Recording(string artistName, string cdName, DateTime release)
        {
            Artist = artistName;
            Name = cdName;
            ReleaseDate = release;
        }
        public string Artist { get; set; }
        public string Name { get; set; }
        public DateTime ReleaseDate { get; set; }
    }
}

<!-- 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="https://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    x:Class="ACBValueMemberPath.MainPage" 
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:ACBValueMemberPath"
    Width="400" Height="413">
    <StackPanel x:Name="LayoutRoot" Background="White" >
        <StackPanel Orientation="Horizontal">
         <TextBlock Margin="5" Text="Enter a recording name:" TextWrapping="Wrap" />
            <sdk:AutoCompleteBox FilterMode="ContainsOrdinal" VerticalAlignment="Top" Margin="5" Width="170" 
                Height="30" x:Name="myACB" 
                ItemsSource="{Binding}" 
                ValueMemberPath="Name">
                <sdk:AutoCompleteBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                        <TextBlock Text="{Binding Path=Name}"  />
                        <TextBlock Text="{Binding Path=Artist}"  />
                        </StackPanel>
                    </DataTemplate>
                </sdk:AutoCompleteBox.ItemTemplate>
            </sdk:AutoCompleteBox>
        </StackPanel>
      </StackPanel>
</UserControl>

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.