Autor Beitrag
Dr.Prof.Evil
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Do 26.02.15 21:37 
Hallo,

ich habe mal wieder ein kleines Problemchen mit dem Binden von einem Wert zu einem Objekt.

Und zwar, möchte ich einen String o.ä. an eine RichTextBox binden. Ich habe schnell gemerkt, dass das nicht so einfach geht,
und habe mich also dann mal im Internet informiert.
Habe eine nette kleine Denkhilfe gefunden und den Code erst einmal übernommen.

ausblenden volle Höhe C#-Quelltext
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:
/*This simply takes the string and reads it line by line. If we have the desired characters at the end of a line (“:.”), 
         * then we make the line blue and bold and remove the characters, otherwise we just add the text. 
         * Each line is added as a paragraph so to reduce the space between each one. 
         http://www.codeproject.com/Articles/137209/Binding-and-styling-text-to-a-RichTextBox-in-WPF*/

        protected Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            FlowDocument doc = new FlowDocument ();

            String s = value as String;

            if(s != null)
            {
                using ( StringReader reader = new StringReader ( s ) )
                {
                    String newLine;
                    while ( ( newLine = reader.ReadLine () ) != null )
                    {
                        Paragraph paragraph = null;
                        if ( newLine.EndsWith ( ":." ) == true )
                        {
                            paragraph = new Paragraph ( new Run ( newLine.Replace ( ":."string.Empty ) ) );
                            paragraph.Foreground = new SolidColorBrush ( Colors.Blue );
                            paragraph.FontWeight = FontWeights.Bold;
                        }
                        else
                            paragraph = new Paragraph ( new Run ( newLine ) );

                        doc.Blocks.Add ( paragraph );
                    }
                }
            }

            return doc;
        }


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
 public class BindableRichTextBox : RichTextBox
    {
        public static readonly DependencyProperty DocumentProperty = DependencyProperty.Register ( "Document"typeof ( FlowDocument ), typeof ( BindableRichTextBox ), new FrameworkPropertyMetadata ( nullnew PropertyChangedCallback ( OnDocumentChanged ) ) );
    
        public new FlowDocument Document
        {
            get
            {
                return (FlowDocument) this.GetValue ( DocumentProperty );
            }
            set
            {
                this.SetValue ( DocumentProperty, value );
            }
        }

        public static void OnDocumentChanged ( DependencyObject obj, DependencyPropertyChangedEventArgs args )
        {
            RichTextBox rtb = (RichTextBox) obj;
            rtb.Document = (FlowDocument) args.NewValue;
        }
    }


Verstehe tuhe ich dies größtenteils. Doch wurde die Anwendung im XAML Code nicht direkt mehr erklärt, und direkt an ein FlowDocument binden klappt auch nicht so direkt.

Ich hoffe ihr versteht mein Problem und einer kann mir weiter helfen oder zu mindestens eine Denkstütze sein. Danke