Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
Wpf 将类成员绑定到列表框_Wpf_List_Xaml_Binding_Listbox - Fatal编程技术网

Wpf 将类成员绑定到列表框

Wpf 将类成员绑定到列表框,wpf,list,xaml,binding,listbox,Wpf,List,Xaml,Binding,Listbox,我是wpf的初学者。在我的一个项目中,我有一个frmField类,如下所示: public class frmFields { public bool succelssfulEnd = true; public string fileName; public List<string> errList = new List<string>(); } 和一份清单 <ListBox Name="LSTErrors" Grid.Row="11"

我是wpf的初学者。在我的一个项目中,我有一个frmField类,如下所示:

public class frmFields
{
    public bool succelssfulEnd = true;
    public string fileName;
    public List<string> errList = new List<string>();
}  
和一份清单

<ListBox Name="LSTErrors" Grid.Row="11" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource ="{Binding}"  SelectionChanged="LSTErrors_SelectionChanged" />
我需要将errList绑定到上面的listBox,这样列表中的任何更改都会反映在listBox上


有人指导我吗?

您需要一个可观察的集合,以便在每次UI更改时刷新它

public class frmFields
{
    public bool succelssfulEnd = true;
    public string fileName;

    private ObservableCollection<String> _errList = new ObservableCollection<String>();
    public ObservableCollection<String> ErrList {
        get {return _errList;}
        set {
            _errList = value;
            //your property changed if you need one
        }
    }
}
或者在您的XAML中:

DataContext="{Binding RelativeSource={RelativeSource self}}"
在根元素中。然后像这样绑定ItemsSource:

<ListBox ItemsSource="{Binding ErrList, Mode=OneWay}">
     ...
</ListBox>

如果你能阅读VB.Net,那么我有一个以前的帮助。不要忘记设置窗口的DataContext,ListBox将自动继承该DataContext,除非您首先在可视化树中的某个位置重置它。并使用属性或依赖项属性,而不是字段。
<ListBox ItemsSource="{Binding ErrList, Mode=OneWay}">
     ...
</ListBox>