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

zrfyljdw  于 2023-05-08  发布在  C#
关注(0)|答案(3)|浏览(167)

我已经创建了简单的数据表机制xaml看起来很简单:

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

MVVM代码基于数据表,并且相当简单:

private void GenerateDataView()
{
    CurrentsFlagValuesView = new DataTable();
    CurrentsFlagValuesView.Columns.Add("Bits");

    var bitLength = 0;

    foreach (CurrentsFlagAnalysis flag in CurrentsFlagValues)
    {
        CurrentsFlagValuesView.Columns.Add(flag.DailyCurrentsTimestampInterval.ToString("yyyy-MM-dd"));
        bitLength = flag.CurrentFlagsLength;
    }

    for (var bit = 0; bit < bitLength; bit++)
    {
        List<CurrentFlagEventEnum> flags = CurrentsFlagValues
            .Select(value => value.CurrentFlags.ElementAt(bit))
            .Select(value => value ? (CurrentFlagEventEnum)bit + 1 : CurrentFlagEventEnum.None)
            .ToList();

        var dataRowValues = new List<object> { bit };
        dataRowValues.AddRange(flags.Cast<object>());

        CurrentsFlagValuesView.Rows.Add(dataRowValues.ToArray());
    }
}

但我遇到了一个问题,或两个我想得到的数据,该单元格时,我点击的细胞,如列标题,和值的细胞。我在没有MVVM的情况下做到了这一点,就像:

void EditingDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
    DataGridCell Cell = EditingDataGrid.GetCurrentDataGridCell();
    var Position = Cell.PointToScreen(new Point(0, 0));

    TextBlock text = (TextBlock)Cell.Content;

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

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

但现在我想把它改造成那种模式,但我不知道怎么做。有什么办法把命令绑定到上面吗?

rkue9o1l

rkue9o1l1#

您可以将DataGridCurrentCell属性绑定到DataGridCellInfo(而不是DataGridCell)源属性,前提是您将BindingMode设置为两个TwoWay

<DataGrid ItemsSource="{Binding CurrentsFlagValuesView}" 
          CurrentCell="{Binding CurrentCell, Mode=TwoWay}"
          AutoGenerateColumns="True" />

然后,每当您在视图中选择一个单元格时,视图模型的source属性就会被设置,您可以简单地将当前逻辑移动到视图模型:

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

您也可以将此功能 Package 在将视图模型的source属性设置为actuall单元格值的行为中:
https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPFhttps://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/

lg40wkob

lg40wkob2#

您可以简单地在视图模型中绑定当前单元格属性,并且您将始终拥有当前单元格:

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

在视图模型中:

private DataGridCell cellInfo;
public DataGridCell CellInfo
{
    get { return cellInfo; }
}
jfgube3f

jfgube3f3#

返回网格中从0开始单击的行

int currentRowIndex = dataGrid1.Items.IndexOf(dtGrid.CurrentItem);

获取列0的currentRowIndex值或特定单元格值

object cellContent = dataGrid1.Columns[0].GetCellContent(dtGrid.Items[currentRowIndex]);
string valueOfColumn0currentRowIndex = (cellContent as TextBlock)?.Text;

相关问题