Silverlight DataGrid, как получить значение ячейки из выбранного элемента? - PullRequest
4 голосов
/ 25 июня 2010

Я пытаюсь получить значение ячейки из выбранного элемента сетки данных silverlight.В прилагаемом коде я могу получить свойства ячейки и изменить ее основной цвет, но не могу получить значение ячейкиМожет кто-нибудь, пожалуйста, дайте мне знать, что я делаю неправильно?Заранее большое спасибо за помощь!

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

Ответы [ 4 ]

4 голосов
/ 26 июня 2010

Спасибо VooDooChild, см. Ниже мое решение с использованием текстового блока для получения значения.

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);
                }
            }
        }
    }
1 голос
/ 27 октября 2011

Вы пробовали что-то вроде этого псевдо:

string myString = ((MyNamespace.MyEntity)(myDataGrid.SelectedItem)).myStringProperty;
1 голос
/ 25 июня 2010
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
  }
}
0 голосов
/ 25 июня 2010

Попробуйте ячейку. Содержание

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

...