Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto bind TextBox control to a StringBuilder instance?

I would like several textboxes to react to changes of an underlying string. So if I were to change the content of the string, all those textboxes would change their content too.

Now, I can't use the String type for that as it is immutable. So I went with StringBuilder. But the Text property of a TextBox object only takes String.

Is there an easy way to "bind" the StringBuilder object to the Text property of a TextBox?

Many thanks!

PS: The TextBox is currently WPF. But I might switch to Windows Forms because of Mono.

like image 805
user51710 Avatar asked Jan 05 '09 16:01

user51710


People also ask

Which property is used to bind the data to textbox?

Path property to specify the value to use for your binding. If you're binding to XML data, you use the Binding. XPath property to specify the value. In some cases, it may be applicable to use the Path property even when your data is XML.

What is System Text StringBuilder in C#?

The System. Text. StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.

Is StringBuilder in C# thread safe?

StringBuilder is also not thread-safe, so operating on it with concurrent threads is illegal. The ReadOnlyMemory<T> chunks returned are not guaranteed to remain unchanged if the StringBuilder is modified, so do not cache them for later use.

What is the namespace required for StringBuilder class?

StringBuilder is located in the System. Text namespace.


2 Answers

You could always expose a property that's getter returns the ToString() of the Stringbuilder. The form could then bind to this property.

private StringBuilder _myStringBuilder;

public string MyText
{
  get { return _myStringBuilder.ToString(); }
}
like image 132
Ray Booysen Avatar answered Sep 19 '22 14:09

Ray Booysen


Here what I use to bind StringBuilder to TextBox in WPF:

public class BindableStringBuilder : INotifyPropertyChanged
{
    private readonly StringBuilder _builder = new StringBuilder();

    private EventHandler<EventArgs> TextChanged;

    public string Text
    {
        get { return _builder.ToString(); }
    }

    public int Count
    {
        get { return _builder.Length; }
    }

    public void Append(string text)
    {
        _builder.Append(text);
        if (TextChanged != null)
            TextChanged(this, null);
        RaisePropertyChanged(() => Text);
    }

    public void AppendLine(string text)
    {
        _builder.AppendLine(text);
        if (TextChanged != null)
            TextChanged(this, null);
        RaisePropertyChanged(() => Text);
    }

    public void Clear()
    {
        _builder.Clear();
        if (TextChanged != null)
            TextChanged(this, null);
        RaisePropertyChanged(() => Text);
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
    {
        if (propertyExpression == null)
        {
            return;
        }

        var handler = PropertyChanged;

        if (handler != null)
        {
            var body = propertyExpression.Body as MemberExpression;
            if (body != null)
                handler(this, new PropertyChangedEventArgs(body.Member.Name));
        }
    }

    #endregion


}

In ViewModel:

public BindableStringBuilder ErrorMessages { get; set; }
ErrorMessages.AppendLine("Missing Image: " + imagePath);

In Xaml:

<TextBox Text="{Binding ErrorMessages.Text, Mode=OneWay}"/>

Of course you can expose other StringBuilder methods if you need.

like image 41
Saykor Avatar answered Sep 16 '22 14:09

Saykor