有没有办法检查网格单元格而不是数据网格单元格是否包含对象

Is there a way to check if a grid cell, NOT datagrid cell, contains an object

我正在将文本框分配给网格中的单元格,但我想在分配之前确认单元格中是否已存在对象。如果为空,是否可以在 returns null 的特定列中查询一行?

我可以创建一个列表列表来表示我在添加和删除对象时修改的网格,但这听起来效率很低。

我写的示例代码:

TextBlock _text = new TextBlock()
                {
                    Text = _cont,
                    Background = new SolidColorBrush(_colo.disciplinecolor)
                }; TextBlockStyle(_text);
                int index = SearchDate((DateTime)_dt);
                Grid.SetRow(_text, 1); Grid.SetColumn(_text, index);
                Maindispgrid.Children.Add(_text);

本质上,每次用户单击按钮时都会调用此代码块,其中 TextBlock 添加到日期列(由用户预先选择),并且希望是列中的下一个可用行。我试过 GetRow(),但这个由 UIElement 搜索的结果似乎不起作用,因为所有 TextBlock 都是用相同的名称创建的。

我可能完全错误地处理了这一切,所以任何关于我需要阅读的内容的线索都将不胜感激。

基本上最终结果应该是这样的:

TextBlock _text = new TextBlock()
                {
                    Text = _cont,
                    Background = new SolidColorBrush(_colo.disciplinecolor)
                }; TextBlockStyle(_text);
                int index = SearchDate((DateTime)_dt);
                //check for next available row at specific column index
                Grid.SetRow(_text, nextAvailableRow); Grid.SetColumn(_text, index);
                Maindispgrid.Children.Add(_text);

您可以通过 Maindispgrid.Children 遍历 children 列表。对于每个 child,检索分配的 RowColumn 值(如果分配的话)。使用此信息计算可用行。

IList<int[]> indices = new List<int[]>();
foreach (var child in Maindispgrid.Children)
{
    int rowIndex = Grid.GetRow(child);
    int columnIndex = Grid.GetColumn(child);
    indices.Add(new int[] {rowIndex, columnIndex});
}
// work with the "indices" list