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();
            }
        }
    }