IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Un RichTextBox bindable


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Mars 2002
    Messages
    118
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 118
    Par défaut Un RichTextBox bindable
    Salut tout le monde!

    Comme certain d'entre vous le savent, le contrôle RichTextBox ne supporte pas le databinding... C'est vraiment moche, mais bon... J'ai décidié de créer mon propre RichTextBox bindable, cependant j'ai quelques problèmes...

    J'ai créé la classe suivant qui fonctionne jusqu'à un certain point... Je m'explique. Tant et aussi longtemps que je l'utilise pour simplement du texte, tout va bien. Mais si j'y copie une image (quelque chose d'assez gros), l'application se meurt jusqu'à planter complètement.

    Pour stoker l'information sur le contrôle, j'utilise le format RTF. Je me suis créé une propriété Content qui contient le RTF du document.

    Est-ce que quelqu'un à une idée de mon problème? Est-ce que quelqu'un à déjà fait ce genre de contrôle?

    Merci de votre aide!

    Merci !

    Martin

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
        public class RichTextBoxEx : RichTextBox
        {
            // Constants ==========================================================
            private const string ContentPropertyName = "Content";
     
            // Attributes =========================================================
            public static readonly DependencyProperty ContentProperty =
                DependencyProperty.Register(ContentPropertyName, typeof(String), typeof(RichTextBoxEx),
                    new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, ContentChanged, null, true, System.Windows.Data.UpdateSourceTrigger.Explicit));
     
            public static bool isNewContent;
     
            // Properties =========================================================
            public string Content
            {
                get { return (string) this.GetValue(ContentProperty); }
                set { this.SetValue(ContentProperty, value); }
            }
     
            // Constructor ========================================================
            public RichTextBoxEx() 
            {
                isNewContent = true;
                this.LostFocus += new RoutedEventHandler(RichTextBoxEx_LostFocus);
            }
     
            // Events Handling ====================================================
            void RichTextBoxEx_LostFocus(object sender, RoutedEventArgs e)
            {
                isNewContent = false;
                this.Content = this.GetRtf();
                isNewContent = true;
            }
     
            // Methods ============================================================
            private static void ContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                if (isNewContent && d is RichTextBoxEx)
                {
                    // BindingExpression be = this.GetBindingExpression(ContentProperty);
                    // be.UpdateSource();
     
                    ((RichTextBoxEx)d).SetRtf((string)e.NewValue);
                }
            }
     
            private void SetTextRange(string content, string dataFormat)
            {
                TextRange textRange = new TextRange(this.Document.ContentStart, this.Document.ContentEnd);
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (StreamWriter streamWriter = new StreamWriter(memoryStream))
                    {
                        streamWriter.Write(content);
                        streamWriter.Flush();
                        memoryStream.Seek(0, SeekOrigin.Begin);
     
                        //Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
                        textRange.Load(memoryStream, dataFormat);
                    }
                }
            }
     
            public void Clear()
            {
                this.Document.Blocks.Clear();
            }
     
            public void SetText(string content)
            {
                SetTextRange(content, DataFormats.Text);
            }
     
            public string GetText()
            {
                // Use a TextRange to fish out the Text from the Document
                return new TextRange(this.Document.ContentStart, this.Document.ContentEnd).Text;
            }
     
            public void SetRtf(string content)
            {
                SetTextRange(content, DataFormats.Rtf);
            }
     
            public string GetRtf()
            {
                string rtf;
     
                TextRange sourceDocument = new TextRange(this.Document.ContentStart, this.Document.ContentEnd);
                using (MemoryStream stream = new MemoryStream())
                {
                    sourceDocument.Save(stream, DataFormats.Rtf);
                    stream.Seek(0, SeekOrigin.Begin);
     
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        rtf = reader.ReadToEnd();
                    }
                }
     
                return rtf;
            }
     
            public void SaveRtf(string filename)
            {
                TextRange range = new TextRange(this.Document.ContentStart, this.Document.ContentEnd);
                using (FileStream fileStream = new FileStream(filename, FileMode.Create)) 
                {
                    range.Save(fileStream, DataFormats.Rtf);
                    fileStream.Close();
                }
            }
        }

  2. #2
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Par défaut
    En débuggant, tu vois pas là où le pb se pose ?

Discussions similaires

  1. [VB6] Ne pas faire de retour automatique dans un RichTextBox
    Par Arthaniel dans le forum VB 6 et antérieur
    Réponses: 5
    Dernier message: 20/01/2005, 03h00
  2. [VB.NET] Marge & Richtextbox
    Par Berns dans le forum Windows Forms
    Réponses: 4
    Dernier message: 29/09/2004, 09h45
  3. [VB.NET] Pb avec le soulignement dans un RichTextBox
    Par Ludog35 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 09/06/2004, 18h59
  4. [VB6] Ecrire à un endroit précis d'un richtextbox
    Par STG dans le forum VB 6 et antérieur
    Réponses: 8
    Dernier message: 26/11/2002, 14h35
  5. [VB6] [RichTextBox] Aller en bas
    Par fea dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 22/10/2002, 11h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo