Autor Beitrag
Schafschaf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 63
Erhaltene Danke: 2

Windows 10
C# (VS 2015)
BeitragVerfasst: Fr 04.03.16 14:12 
Hallo,

ich brauche eigentlich nur eine mehrzeilige TextBox mit WholeWordWrapping. Da das mit der normalen TextBox nicht geht, muss ich die RichEditBox benutzen.
Leider kommt man mit der RichEditBox ohne Verrenkungen nicht an den Text ran, da sie (warum auch immer) kein Text Property hat. Deshlab wollte ich mir eins dazubauen.
Bis jetzt sieht das so aus:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
    class RichTextBox : RichEditBox
    {
        public string Text
        {
            get
            {
                string text;
                Document.GetText(Windows.UI.Text.TextGetOptions.None, out text);
                return text;
            }
            set
            {
                Document.SetText(Windows.UI.Text.TextSetOptions.None, value);
            }
        }
    }


Über den Code Behind lässt sich das zuweisen wie bei einer normalen TextBox wenn ich die im XAML Code definiere.
Nun wollte ich das aber über ein Binding machen, das funktioniert damit aber nicht, wenn ich "Text" an einen string binde.
Wie kann ich das "Text" Property bindable machen?

LG Schafschaf
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 04.03.16 15:49 
Hallo,

so wie bei jedem Binding, d.h. INotifyPropertyChanged implementieren. ;-)
Schafschaf Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 63
Erhaltene Danke: 2

Windows 10
C# (VS 2015)
BeitragVerfasst: Mo 07.03.16 09:31 
Ja da sowieso ;)
Das hat nur leider nix gebracht :(

Habe herausgefunden, dass man es als Dependency Property angeben muss, das habe ich
auch versucht, hat auch nicht funktioniert :(

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:
23:
24:
25:
26:
27:
28:
29:
30:
    class RichTextBox : RichEditBox, INotifyPropertyChanged
    {

        private string text;
        public string Text
        {
            get
            {                
                Document.GetText(Windows.UI.Text.TextGetOptions.None, out text);
                return text;
            }
            set
            {
                Document.SetText(Windows.UI.Text.TextSetOptions.None, value);
                text = value;
                OnPropertyChanged("Text");
            }
        }

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text"typeof(string),
                                                                                             typeof(RichTextBox), new PropertyMetadata(String.Empty));

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(thisnew PropertyChangedEventArgs(prop));
        }
    }
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 07.03.16 10:52 
INotifyPropertyChanged gehört in die Modelklasse nicht das Control.
Schafschaf Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 63
Erhaltene Danke: 2

Windows 10
C# (VS 2015)
BeitragVerfasst: Fr 18.03.16 11:51 
Bin jetzt mittlerweile so weit dass ich einen String an das Textproperty binden kann. PropertyChanged funktioniert auch.

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:
23:
24:
25:
26:
27:
 class RichTextBox : RichEditBox
    {    
                
        public string Text
        {
            get
            {
                return GetValue(TextProperty).ToString();
            }
            set
            {
                SetValue(TextProperty, value);                                
            }
        }

     
        public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text"typeof(string),
        typeof(RichTextBox), new PropertyMetadata(String.Empty,OnChanged));

        static void OnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {                        
            ((RichTextBox)sender).Document.SetText(Windows.UI.Text.TextSetOptions.None,(string)e.NewValue);
        }

        
    }


Leider funktioniert damit kein Two Way Binding. Weiß jemand was ich dafür noch machen muss?
Schafschaf Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 63
Erhaltene Danke: 2

Windows 10
C# (VS 2015)
BeitragVerfasst: Fr 18.03.16 13:54 
So, habs jetzt endlich hinbekommen :)
Für alle, die eine RichEditBox auch per TwoWay an einen string binden wollen:
Den bool braucht man, sonst ruft Textchanged und Onchanged sich gegenseitig auf und man hat eine Endlosschleife.

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:
35:
36:
37:
38:
39:
40:
41:
class RichTextBox : RichEditBox
    {    
        public RichTextBox()
        {
            changed = false;
            TextChanged += RichTextBox_TextChanged;
        }

        private void RichTextBox_TextChanged(object sender, RoutedEventArgs e)
        {
            Document.GetText(Windows.UI.Text.TextGetOptions.None, out text);
            changed = true;           
            SetValue(TextProperty, text);
            changed = false;
        }

        public string Text
        {
            get
            {
                return GetValue(TextProperty).ToString();
            }
            set
            {
                SetValue(TextProperty, value);                                                     
            }
        }

        string text;
        public bool changed;
     
        public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text"typeof(string),
        typeof(RichTextBox), new PropertyMetadata(String.Empty,OnChanged));

        static void OnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (!((RichTextBox)sender).changed)
                ((RichTextBox)sender).Document.SetText(Windows.UI.Text.TextSetOptions.None, (string)e.NewValue);            
        }                
    }