Monday, September 17, 2012

How to get the selected cell value in silverlight using MVVM

How to get the selected cell value in silverlight using MVVM

I have a datagrid which has five columns. i want the cell's value selected by user. I am able to get the selected row index but not column index.

Answers & Comments...

Answer: 1

One way to get the column index of the current cell is to subclass DataGrid and add an extra dependency property:

public class ExtendedDataGrid : DataGrid {     public ExtendedDataGrid() :         base()     {         CurrentCellChanged += ExtendedDataGrid_CurrentCellChanged;     }      public static readonly DependencyProperty SelectedColumnIndexProperty =         DependencyProperty.Register("SelectedColumnIndex", typeof(int), typeof(ExtendedDataGrid), null);      public int SelectedColumnIndex     {         get { return (int)GetValue(SelectedColumnIndexProperty); }         set { SetValue(SelectedColumnIndexProperty, value); }     }      private void ExtendedDataGrid_CurrentCellChanged(object sender, EventArgs e)     {         SelectedColumnIndex = Columns.IndexOf(CurrentColumn);     } } 

This solution relies on the CurrentCellChanged event to update the index of the selected column whenever the current cell changes.

This solution returns the same index for the same column regardless of any reordering of the columns. In other words, if one column has index 2, and you drag the column elsewhere in the grid, it will still have index 2.

by : Luke Woodwardhttp://stackoverflow.com/users/48503




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog