Selector.SelectedValue Property

May 11, 2014

Gets or sets the value of the selected item, obtained by using the SelectedValuePath.

Namespace:  System.Windows.Controls.Primitives
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

public Object SelectedValue { get; set; }
<selector SelectedValue="valueAsString"/>

XAML Values

valueAsString

A string that is equivalent to the desired value as displayed in UI.

Property Value

Type: System.Object
The value of the selected item, obtained by using the SelectedValuePath, or null if no item is selected. The default value is null.

Dependency property identifier field: SelectedValueProperty

Getting the value of SelectedValue returns the value of the SelectedItem, using SelectedValuePath. 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.

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.


public partial class MainPage : PhoneApplicationPage
{

    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("Windows Phone 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; }

}



    <phone:PhoneApplicationPage x:Class="SelectedValuePath.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <StackPanel x:Name="LayoutRoot" Background="Transparent" 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>
</phone:PhoneApplicationPage>


Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show:
© 2014 Microsoft