Hi,
The simplest way is as following:
MainWindow.cs:
<Grid>
<DataGrid x:Name="grid" ItemsSource="{Binding Items}" SelectionChanged="DataGrid_SelectionChanged"/>
</Grid>
Code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
var list = new List<Data>();
for (int i = 0; i < 10; i++)
{
list.Add(new Data() { FirstName = "First " + i,
LastName = "Last " + i });
}
this.Items = list;
}
public IEnumerable Items
{
get;
set;
}
void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = grid.SelectedItems;
}
}
public class Data
{
public string FirstName { get; set; }
public string LastName { get; set; }
}