Hi I am working on a WPF project in which I have a grid control . The itemsSource property of the grid control is bound to a datatable in my viewmodel. I am following the mvvm pattern, so my question is that I need to bind the selectedcell property of the grid control to a property in my view model class. Is it possible to determine the name of the column in which the cell resides by binding it to a property in the view model class. I know an event handler can be attached to the cell which would call a function in the code behind the view, but I dont wish to follow that approach since it would not be mvvm. Kindly help me with any suggestions.
Asked
Active
Viewed 449 times
1 Answers
0
In your XAML bind the CurrentCell
property to a DataGridCellInfo
in your View Model:
<DataGrid SelectionUnit="Cell"
SelectionMode="Single"
ItemsSource="{Binding MyDataTable}"
CurrentCell="{Binding SelectedCellInfo, Mode=OneWayToSource}"/>
Then in your View Model you can access the header from the bound object:
public DataGridCellInfo SelectedCellInfo
{
get { return _selectedCellInfo; }
set
{
_selectedCellInfo = value;
OnPropertyChanged("SelectedCellInfo");
_columnName = _selectedCellInfo.Column.Header;
}
}

RagtimeWilly
- 5,265
- 3
- 25
- 41
-
Thank you for your answer, however am using dev express gridcontrol in my xaml not datagrid, so this does not work for me – user3821877 Mar 17 '15 at 02:58
-
Have you seen this example: https://www.devexpress.com/Support/Center/Example/Details/E3139 – RagtimeWilly Mar 17 '15 at 03:24
-
I got the answer by implementing the FocusedRow and FocusedColumn property, Anyway thanks for your help mate! – user3821877 Mar 28 '15 at 21:41