Condividi tramite


DataGrid.SelectedItem Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the data item corresponding to the selected row.

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

Syntax

'Declaration
Public Property SelectedItem As Object
public Object SelectedItem { get; set; }

Property Value

Type: System.Object
The data item corresponding to the selected row.

Exceptions

Exception Condition
InvalidOperationException

When setting this property to a new value while the control is in editing mode, the edit cannot be committed or reverted.

Remarks

Dependency property identifier field: SelectedItemProperty

If the SelectionMode property is set to Extended and multiple rows are selected, use the SelectedItems property to retrieve all selected items.

If multiple rows are selected and the current row is a selected row, the SelectedItem property returns the current row. If multiple rows are selected and the current row is not a selected row, the SelectedItem property returns the selected row with the lowest index.

Examples

The following example shows how to retrieve the value of the current cell using the CurrentColumn, SelectedItem and CurrentCellChanged members.

Run this sample

Partial Public Class MainPage
    Inherits UserControl
    Public Sub New()
        InitializeComponent()
        Dim myColors As New System.Collections.ObjectModel.ObservableCollection(Of Color)()
        myColors.Add(Colors.Red)
        myColors.Add(Colors.Blue)
        myColors.Add(Colors.Green)
        dataGrid1.DataContext = myColors
    End Sub

    Private Sub dataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As EventArgs)
        If dataGrid1.SelectedItem IsNot Nothing Then
            textBox1.DataContext = (dataGrid1.CurrentColumn.Header.ToString() & ": ") +
                DirectCast(dataGrid1.CurrentColumn.GetCellContent(dataGrid1.SelectedItem), TextBlock).Text
        End If
    End Sub

End Class
public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        System.Collections.ObjectModel.ObservableCollection<Color> myColors =
            new System.Collections.ObjectModel.ObservableCollection<Color>(){Colors.Red, Colors.Blue, Colors.Green};
        dataGrid1.DataContext = myColors;
    }

    private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
    {
        if (dataGrid1.SelectedItem != null)
        {
            textBox1.DataContext = dataGrid1.CurrentColumn.Header.ToString() + ": " + 
                ((TextBlock)dataGrid1.CurrentColumn.GetCellContent(dataGrid1.SelectedItem)).Text;
        }
    }

}
<Grid x:Name="LayoutRoot" Background="White">
    <sdk:DataGrid Name="dataGrid1" AutoGenerateColumns="True" 
        CurrentCellChanged="dataGrid1_CurrentCellChanged"  
        SelectionMode="Single" ItemsSource="{Binding}"
        Height="129" Width="225" Margin="28,34,0,0" 
        HorizontalAlignment="Left" VerticalAlignment="Top" />
    <TextBox Name="textBox1" Height="24" Width="84" IsReadOnly="True" 
        HorizontalAlignment="Left" VerticalAlignment="Top" 
        Margin="259,83,0,0" Text="{Binding}" />
</Grid>

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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