use*_*110 6 c# string data-binding richtextbox winforms
尝试将String绑定到RichTextBox.Text属性,以便在String值更改时,该更改将反映在RichTextBox中.到目前为止,我没有成功.
string test = "Test";
rtxt_chatLog.DataBindings.Add("Text",test,null);
test = "a";
Run Code Online (Sandbox Code Playgroud)
这在rtxt_chatLog中显示"Test",但不显示"a".
甚至尝试添加rtxt_chatLog.Refresh(); 但这没有任何区别.
更新1:这也不起作用:
public class Test
{
public string Property { get; set; }
}
Test t = new Test();
t.Property = "test";
rtxt_chatLog.DataBindings.Add("Text", t, "Property");
t.Property = "a";
Run Code Online (Sandbox Code Playgroud)
我不正确理解数据绑定吗?
本String类没有实现INotifyPropertyChanged,所以有没有活动的绑定源告诉RichTextBox中的东西改变了。
尝试使用已INotifyPropertyChanged实现的类更新您的类:
public class Test : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private string _PropertyText = string.Empty;
public string PropertyText {
get { return _PropertyText; }
set {
_PropertyText = value;
OnPropertyChanged("PropertyText");
}
}
private void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
Run Code Online (Sandbox Code Playgroud)
}
此外,它看起来DataBinding不喜欢属性名称的名称“属性”。尝试将其更改为“属性”以外的其他内容。
rtxt_chatLog.DataBindings.Add("Text", t, "PropertyText");
Run Code Online (Sandbox Code Playgroud)