Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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# 使用templatecolumns从datagrid获取所选单元格_C#_Wpf_Xaml_Mvvm_Datagrid - Fatal编程技术网

C# 使用templatecolumns从datagrid获取所选单元格

C# 使用templatecolumns从datagrid获取所选单元格,c#,wpf,xaml,mvvm,datagrid,C#,Wpf,Xaml,Mvvm,Datagrid,我们有一个可观察的集合,它由一个名为Row的自定义类组成,该类在我们的Datagrid中用作itemsource。Row类本身由3种不同的单元格类型组成 public class Row { public TimeCell TimeCell { get; set; } public PositionsCell PositionsCell { get; set; } public TemperatureCell TempCell { get; set; } } 这些单元

我们有一个可观察的集合,它由一个名为Row的自定义类组成,该类在我们的Datagrid中用作itemsource。Row类本身由3种不同的单元格类型组成

public class Row
{

    public TimeCell TimeCell { get; set; }
    public PositionsCell PositionsCell { get; set; }
    public TemperatureCell TempCell { get; set; }
}
这些单元格类型都有父类单元格,其中包含大多数属性。我们的Datagrid由模板列组成,这些模板列表示行中具有相关绑定的每个单元格。
问题是,当我们执行Datagrid Selecteditem时,我们当前得到的是返回的Model.Row,而我们希望直接获得单元格(Model.Row.TimeCell),例如,如果单击/选择了该列中的单元格。我们如何实现这一点?

您需要将DataGrid.SelectedItem转换为自定义类项,即

Row selRow = mainDataGrid.SelectedItem as Row;
现在您可以访问行的属性,即

TimeCell timeCell = selRow.TimeCell;
希望这能奏效


感谢您让我们帮助您

您需要将DataGrid.SelectedItem转换为您的自定义类项目,即

Row selRow = mainDataGrid.SelectedItem as Row;
现在您可以访问行的属性,即

TimeCell timeCell = selRow.TimeCell;
希望这能奏效


感谢您让我们帮助您

检查datagrid的currentcell属性将为您提供您所期望的。检查currentcell将提供控件。DatagridCellinfo,检查currentcell.item将提供与datagrid SelectedItem相同的控件检查此帖子查看了该帖子并测试了所有内容,没有任何效果。其中一个答案指出它不适用于datagridtemplatecolumns。请将
DataGrid.SelectionUnit
设置为
DataGridSelectionUnit.Cell
。使用此配置,
DataGrid。SelectedCells
将返回当前选定的单元格。检查DataGrid的currentcell属性将提供您所期望的内容。检查currentcell将提供控件。DatagridCellinfo,检查currentcell.item与datagrid SelectedItem提供的相同检查此帖子查看该帖子并测试了所有内容,但没有任何效果。其中一个答案指出它不适用于datagridtemplatecolumns。请将
DataGrid.SelectionUnit
设置为
DataGridSelectionUnit.Cell
。使用此配置,
DataGrid.SelectedCells
将返回当前选定的单元格。是的,它可以工作,但不是我们要的。我们想得到当前的手机。是的,它可以工作,但它不是我们想要的。我们想得到当前的单元格。