| Visual Basic (Declaration) | |
|---|---|
Public Property SelectedItem As Object | |
| C# | |
|---|---|
public object SelectedItem {get; set;} | |
Return Value
A reference to the currently selected data item. Can be a null reference (Nothing in Visual Basic) an item other than a master data item is selected.The following example demonstrates how to provide value-changed handlers for the SelectedItem and SelectedIndex dependency properties in order to be notified when their value changes (see Example 1).
The SelectedItems property is also a dependency property; however, using a value-changed handler will not function in this case since the values of the collection are modified and not the collection itself. Although the SelectedItems property is exposed as an IList it is actually an ObservableCollection; therefore, in order to be notified when its content (i.e., the items that are selected) changes, it can be cast as an INotifyCollectionChanged and subscribe to its CollectionChanged event (see Example 2).
| Visual Basic | Copy Code |
|---|---|
Dim selectedItemDescriptor As DependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty( DataGridControl.SelectedItemProperty, GetType( DataGridControl ) ) | |
| C# | Copy Code |
|---|---|
DependencyPropertyDescriptor selectedItemDescriptor = DependencyPropertyDescriptor.FromProperty( DataGridControl.SelectedItemProperty,
typeof( DataGridControl ) ); | |
| Visual Basic | Copy Code |
|---|---|
AddHandler CType( Me.EmployeesGrid.SelectedItems, INotifyCollectionChanged ).CollectionChanged, AddressOf Me.Window1_CollectionChanged | |
| C# | Copy Code |
|---|---|
( ( INotifyCollectionChanged )this.EmployeesGrid.SelectedItems ).CollectionChanged +=
new NotifyCollectionChangedEventHandler( Window1_CollectionChanged ); | |
The SelectedItems, SelectedItem, and SelectedIndex properties represent data items and not DataRows.
In the case where more than 1 data item is selected in a grid, the SelectedItem property will return the first data item in the SelectedItems collection and the SelectedIndex property will return the index of this same item.
The CurrentItem and SelectedItem properties may or may not be the same data item.
The CurrentItem and SelectedItem/SelectedItems properties must be set to a null reference (Nothing in Visual Basic) in order to reset them when the NavigationBehavior property is set to None.
Supported Frameworks: Microsoft .NET Framework version 3.5
Copy Code