Partager via


Selector.SelectedValuePath 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 SelectedValue property of the SelectedItem property.

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

Syntax

'Declaration
Public Property SelectedValuePath As String
public string SelectedValuePath { get; set; }
<selector SelectedValuePath="propertyPath"/>

XAML Values

  • propertyPath
    A string that is evaluated as a simple dot syntax path to an object property.

Property Value

Type: System.String
The property path that is used to get the SelectedValue property of the SelectedItem property. The default is Empty.

Remarks

Dependency property identifier field: SelectedValuePathProperty

SelectedValuePath is used to specify what value is returned by the SelectedValue property. For example, if you have a control derived from Selector that is bound to a collection of objects of type Employee, and Employee has two properties named EmployeeName and EmployeeNumber. You can set SelectedValuePath to "EmployeeNumber" to have SelectedValue return the value of EmployeeNumber.

Examples

The following example shows how to use the SelectedValuePath and SelectedValue properties with a ComboBox control. The DisplayMemberPath is set to the name of the displayed document, and the SelectedValuePath is set to the ID property. In this example, the ID for the selected item is simply displayed on the screen, but in a real application, you could use it to retrieve the selected object.

Run this sample

    Public MyDocs As System.Collections.ObjectModel.ObservableCollection(Of Document) =
        New System.Collections.ObjectModel.ObservableCollection(Of Document)

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        ' Add items to the collection.
        MyDocs.Add(New Document("How to: Use SelectedValuePath"))
        MyDocs.Add(New Document("DataGrid Overview"))
        MyDocs.Add(New Document("Silverlight Designer Overview"))
        comboBox1.DataContext = MyDocs
        textBlock2.DataContext = comboBox1
    End Sub
End Class

' Simple business object.
Public Class Document
    Private nameValue As String
    Private guidValue As Guid

    Public Sub New()
        MyBase.New()

    End Sub

    Public Sub New(ByVal docName As String)
        MyBase.New()
        guidValue = Guid.NewGuid()
        Name = docName
    End Sub

    Public Property Name As String
        Get

            Return nameValue

        End Get
        Set(ByVal value As String)
            nameValue = value
        End Set

    End Property

    Public Property ID As Guid
        Get
            Return guidValue
        End Get
        Set(ByVal value As Guid)
            guidValue = value
        End Set

    End Property
End Class
public partial class MainPage : UserControl
{

    public System.Collections.ObjectModel.ObservableCollection<Document> MyDocs =
        new System.Collections.ObjectModel.ObservableCollection<Document>();
    public MainPage()
    {
        InitializeComponent();

        // Add items to the collection.
        MyDocs.Add(new Document("How to: Use SelectedValuePath"));
        MyDocs.Add(new Document("DataGrid Overview"));
        MyDocs.Add(new Document("Silverlight Designer Overview"));
        comboBox1.DataContext = MyDocs;
        textBlock2.DataContext = comboBox1;
    }
}

// Simple business object.
public class Document
{
    public Document() { }
    public Document(string docName)
    {
        ID = Guid.NewGuid();
        Name = docName;
    }
    public string Name { get; set; }
    public Guid ID { get; set; }

}
    <UserControl x:Class="SelectedValuePath.MainPage"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <StackPanel x:Name="LayoutRoot" Background="White" Height="312" Width="400">
        <ComboBox Height="23" ItemsSource="{Binding}" DisplayMemberPath="Name" 
                  SelectedValuePath="ID" 
                  HorizontalAlignment="Left" Margin="5" Name="comboBox1" 
                   Width="238" />
        <TextBlock Height="18" HorizontalAlignment="Left" Margin="5" 
                   Name="textBlock1" Text="Selected Value (GUID):"  Width="238" />
        <TextBlock Height="32" HorizontalAlignment="Left"  Name="textBlock2" 
                   Width="238" Text="{Binding SelectedValue}"/>
    </StackPanel>
</UserControl>

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

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