WPF:如何检测在DataGrid控件中完成的行渲染?

时间:2019-02-23 12:47:07

标签: c# wpf datagrid

我想添加一列以显示序列号,而不在项目源数据中添加额外的属性,并在绑定属性中绑定序列值。

我在加载行事件中添加了一个事件处理程序,但我注意到在触发此事件期间最后一行的元素尚未初始化。

var dataGrid = new DataGrid();
dataGrid.LoadingRow += (s, e) =>
{
    if (dataGrid.ItemsSource != null && e.Row.GetIndex() + 1 == ((IList)dataGrid.ItemsSource).Count)
    {
        int participantColumnsCount = ((DataGridControlEntity)item).DisplayMembersPathEn.Count + 2;
        var elements = FindVisualChildren<TextBlock>(dataGrid);
        int cellIdx = 0;
        for (int idx = 1; idx <= ((IList)dataGrid.ItemsSource).Count; idx++)
        {
            cellIdx = 0 + (idx * participantColumnsCount);
            if (cellIdx + 1 >= elements.Count())
                break;
            elements.ElementAt(cellIdx).Text = idx.ToString();
        }
    }
};

Result after executing the above code snippet 有任何线索吗?

1 个答案:

答案 0 :(得分:0)

我可以通过订阅LayoutUpdated事件而不是LoadingRow事件来找到解决此问题的票价解决方法。

相关问题