Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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
WPF C#从自动生成的数据网格和数据表中获取单元格值_C#_Wpf - Fatal编程技术网

WPF C#从自动生成的数据网格和数据表中获取单元格值

WPF C#从自动生成的数据网格和数据表中获取单元格值,c#,wpf,C#,Wpf,我为数据表xaml创建了简单的机制,它看起来很简单: <DataGrid ItemsSource="{Binding CurrentsFlagValuesView}" AutoGenerateColumns="True" /> 但现在我想把它改造成那种模式,但我不知道怎么做。您知道如何将命令绑定到它吗?您只需在视图模型中绑定当前单元格属性,就可以一直使用当前单元格: <DataGrid AutoGenerateColumns="True" Selec

我为数据表xaml创建了简单的机制,它看起来很简单:

<DataGrid ItemsSource="{Binding CurrentsFlagValuesView}" AutoGenerateColumns="True" />

但现在我想把它改造成那种模式,但我不知道怎么做。您知道如何将命令绑定到它吗?

您只需在视图模型中绑定当前单元格属性,就可以一直使用当前单元格:

      <DataGrid AutoGenerateColumns="True" 
      SelectionUnit="Cell" 
      CanUserDeleteRows="True" 
      ItemsSource="{Binding Results}" 
      CurrentCell="{Binding CellInfo}"            
      SelectionMode="Single">

您可以将
DataGrid
CurrentCell
属性绑定到
DataGridCellInfo
(非
DataGridCell
)源属性,前提是设置
绑定的
模式

<DataGrid ItemsSource="{Binding CurrentsFlagValuesView}" 
          CurrentCell="{Binding CurrentCell, Mode=TwoWay}"
          AutoGenerateColumns="True" />
还可以将此功能包装为将视图模型的源属性设置为实际单元格值的行为:

      <DataGrid AutoGenerateColumns="True" 
      SelectionUnit="Cell" 
      CanUserDeleteRows="True" 
      ItemsSource="{Binding Results}" 
      CurrentCell="{Binding CellInfo}"            
      SelectionMode="Single">
private DataGridCell cellInfo;
public DataGridCell CellInfo
{
    get { return cellInfo; }
}
<DataGrid ItemsSource="{Binding CurrentsFlagValuesView}" 
          CurrentCell="{Binding CurrentCell, Mode=TwoWay}"
          AutoGenerateColumns="True" />
private DataGridCellInfo _currentCell;
public DataGridCellInfo CurrentCell
{
    get { return _currentCell; }
    set { _currentCell = value; OnCurrentCellChanged(); }
}

void OnCurrentCellChanged()
{
    DataGridCell Cell = GetCurrentDataGridCell(_currentCell);
    var Position = Cell.PointToScreen(new Point(0, 0));

    TextBlock text = (TextBlock)Cell.Content;
    MessageBox.Show("Value=" + text.Text, "Position");
}

public static DataGridCell GetCurrentDataGridCell(DataGridCellInfo cellInfo)
{
    if (cellInfo == null || cellInfo.IsValid == false)
    {
        return null;
    }
    var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
    if (cellContent == null)
    {
        return null;
    }
    return cellContent.Parent as DataGridCell;
}