如何将TextBox.Lines绑定到BindingList<;字符串>;在C#中的WinForms中?
我有如何将TextBox.Lines绑定到BindingList<;字符串>;在C#中的WinForms中?,c#,.net,winforms,data-binding,binding,C#,.net,Winforms,Data Binding,Binding,我有 私有绑定列表日志; 我的表格上有多行日志文本框。 如何将“日志”列表绑定到该文本框 我不需要双向绑定。从日志到文本框的单向绑定就足够了。您不能直接从绑定列表绑定到文本框,因为文本框中的行属性的类型为字符串[]不是绑定列表 您需要一个字符串[]属性,以及一个属性更改通知 下面是一个如何做到这一点的示例 private BindingList<string> log; public class LinesDataSource : INotifyPropertyChanged {
私有绑定列表日志;
我的表格上有多行日志文本框。
如何将“日志”列表绑定到该文本框
我不需要双向绑定。从日志到文本框的单向绑定就足够了。您不能直接从
绑定列表
绑定到文本框
,因为文本框
中的行
属性的类型为字符串[]
不是绑定列表
您需要一个字符串[]
属性,以及一个属性更改通知
下面是一个如何做到这一点的示例
private BindingList<string> log;
public class LinesDataSource : INotifyPropertyChanged
{
private BindingList<string> lines = new BindingList<string>();
public LinesDataSource()
{
lines.ListChanged += (sender, e) => OnPropertyChanged("LinesArray");
}
public BindingList<string> Lines
{
get { return lines; }
}
public string[] LinesArray
{
get
{
return lines.ToArray();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private LinesDataSource dataSource = new LinesDataSource();
private void Setup()
{
textBox.DataBindings.Add("Lines", dataSource, "LinesArray");
Populate();
}
private void Populate()
{
dataSource.Lines.Add("whatever");
}