2016-10-15 6 views

Répondre

1

Vous pouvez cloner l'exécution source dans une exécution cible, puis modifier le texte de l'exécution cible.

// Create a formatted source run 
Run sourceRun = new Run("TextOfSourceRun") { FontWeight = FontWeights.Bold, Background = Brushes.Khaki, Foreground = Brushes.Green, FontSize = 25 }; 

// Clone it 
Run targetRun = ElementClone<Run>(sourceRun); 

// Change the text of the target run 
targetRun.Text = "TextOfTargetRun"; 

// Insert the target run at the end of the current paragraph 
richTextBox.CaretPosition.Paragraph.Inlines.InsertAfter(richTextBox.CaretPosition.Paragraph.Inlines.Last(), targetRun); 

public static T ElementClone<T>(T element) 
{ 
    object clonedElement = null; 

    MemoryStream memStream = new MemoryStream(); 
    XamlWriter.Save(element, memStream); 

    if (memStream.CanRead) 
    { 
     memStream.Seek(0, SeekOrigin.Begin); 
     clonedElement = XamlReader.Load(memStream); 
     memStream.Close(); 
    } 

    return (T)clonedElement; 
}