我是 WPF 菜鸟。 我有一个 listbox,它绑定(bind)到 Client
类的属性public class Client : INotifyPropertyChanged
{
private List<Player> peers ;
public List<Player> Peers
{
get { return peers; }
set
{
peers = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Peers"));
}
}
}
列表框父级的数据上下文绑定(bind)到一个客户端实例
GameDetailsPanel.DataContext = client;
列表框的边界如下:
<ListBox.Items>
<Binding Path="Peers"></Binding>
</ListBox.Items>
据我了解,这是假设将列表绑定(bind)到相对于其父项的路径 datacontext .. 当我运行应用程序时出现以下错误:
{"A 'Binding' cannot be used within a 'ItemCollection' collection. A 'Binding' can only be set on a DependencyProperty of a DependencyObject."}
任何想法我做错了什么。
请您参考如下方法:
您需要将ItemsSource 设置为Players 的List。
<ListBox ItemsSource="{Binding Peers}">
</ListBox>
