Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Selector Class
 SelectedValue Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Selector..::.SelectedValue Property

Gets or sets the value of the SelectedItem, obtained by using SelectedValuePath. This is a dependency property.

Namespace:  System.Windows.Controls.Primitives
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/xaml/presentation
Visual Basic (Declaration)
<LocalizabilityAttribute(LocalizationCategory.NeverLocalize)> _
<BindableAttribute(True)> _
Public Property SelectedValue As Object
Visual Basic (Usage)
Dim instance As Selector
Dim value As Object

value = instance.SelectedValue

instance.SelectedValue = value
C#
[LocalizabilityAttribute(LocalizationCategory.NeverLocalize)]
[BindableAttribute(true)]
public Object SelectedValue { get; set; }
Visual C++
[LocalizabilityAttribute(LocalizationCategory::NeverLocalize)]
[BindableAttribute(true)]
public:
property Object^ SelectedValue {
    Object^ get ();
    void set (Object^ value);
}
JScript
public function get SelectedValue () : Object
public function set SelectedValue (value : Object)
XAML Property Element Usage
<object>
  <object.SelectedValue>
    <Object .../>
  </object.SelectedValue>
</object>

Property Value

Type: System..::.Object
The value of the selected item.

Identifier field

SelectedValueProperty

Metadata properties set to true

BindsTwoWayByDefault, Journal

The SelectedValuePath property specifies the path to the property that is used to determine the value of the SelectedValue property. Setting SelectedValue to a value X attempts to select an item whose value evaluates to X; if no such item can be found, the selection is cleared.

The following example binds a ListBox to a collection of Employee objects. The example sets SelectedValuePath to @EmployeeNumber and SelectedValue to 12345. This selects the Employee that has 12345 as the value of EmployeeNumber. This example also binds a TextBlock to the SelectedValue of the ListBox. When the user changes the selection in the ListBox, the TextBlock is updated to show the employee number of the currently selected employee.

<XmlDataProvider x:Key="Employees" XPath="/Employees/*">
  <x:XData>
    <Employees xmlns="">
      <Employee Name="Terry Adams" Type="FTE" EmployeeNumber="1" />
      <Employee Name="Claire O&apos;Donnell" Type="FTE" EmployeeNumber="12345" />
      <Employee Name="Palle Peterson" Type="FTE" EmployeeNumber="5678" />
      <Employee Name="Amy E. Alberts" Type="CSG" EmployeeNumber="99222" />
      <Employee Name="Stefan Hesse" Type="Vendor" EmployeeNumber="-" />
    </Employees>
  </x:XData>
</XmlDataProvider>

<DataTemplate x:Key="EmployeeItemTemplate">
  <TextBlock Text="{Binding XPath=@Name}" />
</DataTemplate>


...


<ListBox Name="employeeListBox"
         ItemsSource="{Binding Source={StaticResource Employees}}"
         ItemTemplate="{StaticResource EmployeeItemTemplate}"
         SelectedValue="12345"
         SelectedValuePath="@EmployeeNumber"/>

<TextBlock Text="{Binding ElementName=employeeListBox, 
                  Path=SelectedValue}"/>


Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker