Selector.SelectedItem Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the first item in the current selection or returns null if the selection is empty

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

[BindableAttribute(true)]
public object SelectedItem { get; set; }

Property Value

Type: System.Object

The first item in the current selection or null if the selection is empty.

If a Selector supports selecting a single item, the SelectedItem property returns the selected item. If a Selector supports multiple selections, SelectedItem returns the item that the user selected first.

Setting SelectedItem in a Selector that supports multiple selections clears existing selected items and sets the selection to the item specified.

The following example gets the SelectedItem from a ListBox.

void PrintText(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
    tb.Text = "   You selected " + lbi.Content.ToString() + ".";
}

.NET Framework
Available since 3.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: