Reputation: 9930
I have a WPF datagrid and I'm trying to get the value of my first row for the seleted item. I've tried using the following based on previous questions I've found here, but had no success:
var eventid = dataGridArchiveQueue.SelectedItem;
also tried:
var eventid = dataGridArchiveQueue.Columns[0].GetValue(dataGridArchiveQueue.SelectedItem);
What am I not understanding?
Upvotes: 0
Views: 102
Reputation: 9806
If you are using MVVM, you can bind an object in your view model to the datagrid's SelectedItem property as such;
<DataGrid SelectedItem="{Binding SelectedItemInMyViewModel}" ... >
...
</DataGrid>
Then in your view model, you can expose this property with whatever type you like (meaning you won't need to cast it, but providing it is the type as the Array/List objects you are binding to the DataGrid's ItemsSource
For example if you were binding a List to the datagrid, on the view model you could have;
public Person SelectedItemInMyViewModel { get; set; }
You can then look at that property whenever you like to see the selected item.
Upvotes: 1