4

I am trying to get a cell value from the selected item of a silverlight datagrid. In the attached code I can get to the properties of the cell and change its forecolor, but I can not get the value of the cell. Can someone please let me know what I am doing wrong? Many thanks in advance for your help!

    private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataGrid dataGrid = sender as DataGrid;

        int selectedIndex = dataGrid.SelectedIndex;
        if (selectedIndex > -1)
        {
            FindResult findResult = (FindResult)FindDetailsDataGrid.SelectedItem;

            DataGridColumn column = dataGrid.Columns[0];
            FrameworkElement fe = column.GetCellContent(dataGrid.SelectedItem);
            FrameworkElement result = GetParent(fe, typeof(DataGridCell));

            if (result != null)
            {
                DataGridCell cell = (DataGridCell)result;
                //changes the forecolor
                cell.Foreground = new SolidColorBrush(Colors.Blue);
                //how to get cell value?
            }
        }
    }

    private FrameworkElement GetParent(FrameworkElement child, Type targetType)
    {
        object parent = child.Parent;
        if (parent != null)
        {
            if (parent.GetType() == targetType)
            {
                return (FrameworkElement)parent;
            }
            else
            {
                return GetParent((FrameworkElement)parent, targetType);
            }
        }
        return null;
    }
Arkady
  • 393
  • 2
  • 9
  • 29

4 Answers4

4

Thanks VooDooChild, see below for my solution using the textblock to get at value.

private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataGrid dataGrid = sender as DataGrid;

        int selectedIndex = dataGrid.SelectedIndex;
        if (selectedIndex > -1)
        {
            FindResult findResult = (FindResult)FindDetailsDataGrid.SelectedItem;

            DataGridColumn column = dataGrid.Columns[0];
            FrameworkElement fe = column.GetCellContent(dataGrid.SelectedItem);
            FrameworkElement result = GetParent(fe, typeof(DataGridCell));

            if (result != null)
            {
                DataGridCell cell = (DataGridCell)result;
                //changes the forecolor
                cell.Foreground = new SolidColorBrush(Colors.Blue);
                //how to get cell value?

                TextBlock block = fe as TextBlock;
                if (block != null)
                {
                    string cellText = block.Text;
                    MessageBox.Show(cellText);
                }
            }
        }
    }
Arkady
  • 393
  • 2
  • 9
  • 29
1
private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  DataGrid dataGrid = sender as DataGrid;
  var item = dataGrid.SelectedItem;
  if (item != null)
  {
    //in here you can get the properties with the "item"'s object
  }
}
VoodooChild
  • 9,776
  • 8
  • 66
  • 99
  • Can you show how to retrieve the value from the selected item? I can only change the properties of the textblock. – Arkady Jun 25 '10 at 20:30
1

Have you tried something like this pseudo:

string myString = ((MyNamespace.MyEntity)(myDataGrid.SelectedItem)).myStringProperty;
T.Rob
  • 31,522
  • 9
  • 59
  • 103
0

Try cell.Content

http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx

Biff MaGriff
  • 8,102
  • 9
  • 61
  • 98