Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Silverlight DataGrid如何从选定项获取单元格值?_C#_Silverlight_Datagrid - Fatal编程技术网

C# Silverlight DataGrid如何从选定项获取单元格值?

C# Silverlight DataGrid如何从选定项获取单元格值?,c#,silverlight,datagrid,C#,Silverlight,Datagrid,我试图从silverlight数据网格的选定项中获取单元格值。在附加的代码中,我可以获得单元格的属性并更改其前景色,但我无法获得单元格的值。有人能告诉我我做错了什么吗?非常感谢您的帮助 private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e) { DataGrid dataGrid = sender as DataGrid; int

我试图从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;
    }
试试手机内容


感谢伏都教,下面是我使用文本块获取值的解决方案

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

你有没有试过这样的方法:

string myString = ((MyNamespace.MyEntity)(myDataGrid.SelectedItem)).myStringProperty;

哪个对象绑定到相关单元格的数据网格列?如果您已完成此问题,则应标记此已回答。谢谢。您可以演示如何从所选项目检索值吗?我只能更改textblock的属性。我尝试了cell.Content.ToString();并返回System.Windows.Controls.TextBlock try((TextBlock)cell.Content)。Text:D
string myString = ((MyNamespace.MyEntity)(myDataGrid.SelectedItem)).myStringProperty;