你尝试过grid.RowDefinitions.Count和grid.ColumnDefinitions.Count吗?
grid.RowDefinitions.Count
grid.ColumnDefinitions.Count