Silverlight DataGrid how to get cell value from a selected item?

后端 未结 4 1968
孤街浪徒
孤街浪徒 2021-01-13 23:50

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

4条回答
  •  清酒与你
    2021-01-14 00:30

    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);
                    }
                }
            }
        }
    

提交回复
热议问题