The definition of the selected cell value is more relevant to the WinForms object. WPF is designed to work in different ways; your user interface should be separate from the logic. Thus, DataGrid becomes a tool for presentation, and not something that you need to poke and push towards values.
Instead, with WPF, you want to deal with the objects that you have attached to the grid itself, regardless of how they are displayed. Forget about the grid - just find the object that is currently "selected" by the user from the list of related objects.
SelectedItem is a property of the grid itself and, thanks to the excellent WPF binding mechanisms, you can bind this value to the ViewModel property via XAML:
ItemsSource="{Binding Orders, Mode=OneWay}" SelectedItem="{Binding SelectedOrder, Mode=TwoWay}"
When the user selects an item in the grid, the two-way binding will update the SelectedItem property in the ViewModel with the value of this object in this row.
This way, you donβt even have to deal with knowledge of the grid or user interface.
Hope this makes sense. I know this is a different approach and a different way of thinking coming from WinForms.
Chris Holmes 01 Oct '10 at 12:51
source share