C# WPF将反向代码对象绑定到UI

C# WPF将反向代码对象绑定到UI,c#,wpf,xaml,data-binding,listbox,C#,Wpf,Xaml,Data Binding,Listbox,有人能给我解释一下如何将一个集合,例如一个String[]对象绑定到一个WPFListBox(在xaml中),以及它是如何工作的,因为我在阅读了MSDN和其他网站上的教程后生病了。我还是不知道怎么做 假设MainWindow.xaml.cs中有: String[] bigBadWolf = {"1","2","3","4","5"}; 我希望bigBadWolf与xaml中的ListBox绑定(我希望bigBadWolf的每个成员垂直显示在ListBox中(类似于播放列表))。如果要使用mai

有人能给我解释一下如何将一个集合,例如一个
String[]
对象绑定到一个WPF
ListBox
(在xaml中),以及它是如何工作的,因为我在阅读了MSDN和其他网站上的教程后生病了。我还是不知道怎么做

假设MainWindow.xaml.cs中有:

String[] bigBadWolf = {"1","2","3","4","5"};

我希望
bigBadWolf
与xaml中的
ListBox
绑定(我希望
bigBadWolf
的每个成员垂直显示在
ListBox
中(类似于播放列表))。

如果要使用
main window.xaml.cs
,然后我建议您定义一个
dependencProperty
来绑定:

public static readonly DependencyProperty BigBadWolfProperty = DependencyProperty.
    Register("BigBadWolf", typeof(string[]), typeof(MainWindow), new
    UIPropertyMetadata(100.0));

public string[] BigBadWolf
{
    get { return (string[])GetValue(BigBadWolfProperty); }
    set { SetValue(BigBadWolfProperty, value); }
}    
然后,您必须设置
DataContext
。。。最简单(但不是最好)的方法是在构造函数中执行此操作:

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
}
现在绑定到属性:

<ListBox ItemsSource="{Binding BigBadWolf}" />


请注意,在WPF中更常见的做法是使用
可观测集合
集合。

仍然无法获取它;/在WPF中,无法将集合对象(如list)绑定到ListBox的ItemSource属性。这一切都必须完成?WPF是一种与WinForms或ASP.NET截然不同的语言。如果你想用它来发展,那么你有很多东西要学。