Public Class DlgDocDinamico
  Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    ' Buscar los elementos Span que se pueden cambiar
    Buscar_Paragraph(documento.Blocks)
    For Each block As Block In documento.Blocks
      Dim lista As List = TryCast(block, List)
      If lista IsNot Nothing Then
        For Each listItem As ListItem In lista.ListItems
          Buscar_Paragraph(listItem.Blocks)
        Next
      End If
    Next
  End Sub

  Private Sub Buscar_Paragraph(blockCollection As BlockCollection)
    ' Buscar elementos Paragraph
    For Each block As Block In blockCollection
      Dim parrafo As Paragraph = TryCast(block, Paragraph)
      If parrafo IsNot Nothing Then
        Buscar_Span(parrafo)
      End If
    Next
  End Sub

  Private Sub Buscar_Span(parrafo As Paragraph)
    ' Buscar elementos Span
    For Each inline As Inline In parrafo.Inlines
      Dim span As Span = TryCast(inline, Span)
      If span IsNot Nothing AndAlso inline.Tag IsNot Nothing Then
        ' Añadir una etiqueta y una caja de texto por cada Span
        Dim fila As New RowDefinition()
        gridCambios.RowDefinitions.Add(fila)

        Dim etiqueta As New Label()
        etiqueta.Content = inline.Tag.ToString() & ": "
        Grid.SetColumn(etiqueta, 0)
        Grid.SetRow(etiqueta, gridCambios.RowDefinitions.Count - 1)
        gridCambios.Children.Add(etiqueta)

        Dim cajaTexto As New TextBox()
        Grid.SetColumn(cajaTexto, 1)
        Grid.SetRow(cajaTexto, gridCambios.RowDefinitions.Count - 1)
        cajaTexto.Tag = span.Inlines.FirstInline
        ' elemento Run
        gridCambios.Children.Add(cajaTexto)
      End If
    Next
  End Sub

  Private Sub btCambios_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    For Each elmentoEnGridCambios As UIElement In gridCambios.Children
      If Grid.GetColumn(elmentoEnGridCambios) = 1 Then
        Dim cajaTexto As TextBox = DirectCast(elmentoEnGridCambios, TextBox)
        If cajaTexto.Text <> "" Then
          DirectCast(cajaTexto.Tag, Run).Text = cajaTexto.Text
        End If
      End If
    Next
  End Sub
End Class
