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.
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.
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.
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.
StringBuilder is located in the System. Text namespace.
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(); }
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With