Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将TextBox.Lines绑定到BindingList<;字符串>;在C#中的WinForms中?_C#_.net_Winforms_Data Binding_Binding - Fatal编程技术网

如何将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");
}