WinRt: Liaison d’une chaîne RTF à un RichEditBox

Longtemps recherché pour lier du texte RTF à un contrôle RichEditBox sous Windows Store Applications. Même cela devrait fonctionner en mode reliure à deux mois. …

… enfin j’ai trouvé la solution suivante. J’ai créé un contrôle hérité du contrôle RichEditBox d’origine avec un DependencyProperty RtfText.

public class RichEditBoxExtended : RichEditBox { public static readonly DependencyProperty RtfTextProperty = DependencyProperty.Register( "RtfText", typeof (ssortingng), typeof (RichEditBoxExtended), new PropertyMetadata(default(ssortingng), RtfTextPropertyChanged)); private bool _lockChangeExecution; public RichEditBoxExtended() { TextChanged += RichEditBoxExtended_TextChanged; } public ssortingng RtfText { get { return (ssortingng) GetValue(RtfTextProperty); } set { SetValue(RtfTextProperty, value); } } private void RichEditBoxExtended_TextChanged(object sender, RoutedEventArgs e) { if (!_lockChangeExecution) { _lockChangeExecution = true; ssortingng text; Document.GetText(TextGetOptions.None, out text); if (ssortingng.IsNullOrWhiteSpace(text)) { RtfText = ""; } else { Document.GetText(TextGetOptions.FormatRtf, out text); RtfText = text; } _lockChangeExecution = false; } } private static void RtfTextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { var rtb = dependencyObject as RichEditBoxExtended; if (rtb == null) return; if (!rtb._lockChangeExecution) { rtb._lockChangeExecution = true; rtb.Document.SetText(TextSetOptions.FormatRtf, rtb.RtfText); rtb._lockChangeExecution = false; } } } 

Cette solution fonctionne pour moi, peut-être aussi pour d’autres. 🙂

Problèmes connus: comportements étranges dans VirtualizingStackPanel.VirtualizationMode = “Recycling”