更新:到目前为止,我已根据您的帮助更新了代码,但仍然没有运气。当应用程序加载ListBox时没有项目。我给你的垃圾值,让客户在窗户的构造器,然后我也尝试设置ListBox的DataContext的如下:将收藏绑定到WPF列表框
CustomerList.DataContext = Customers;
--- 原始的问题(与更新的代码)---
我在WPF项目中遇到数据绑定问题。
我有一个类,客户,如下:
public class Customer
{
public String Name { get; set; }
public String Email { get; set; }
}
在我的XAML代码后面我有客户的集合如下:
public List<Customer> Customers { get; set; }
我想每一个客户绑定到带有ListItemTemplate的ListBox,在TextBox中显示客户的信息(名称/电子邮件)以及锁定/解除文本框(将IsEnabled属性设置为true或false)的按钮。
这是什么最好的方法呢?
到目前为止,我一直在试图他没有成功。
在XAML我目前有以下(忽略现在切换的一部分,我只是试图让自己要列出的集合。):
<Window.Resources>
<CollectionViewSource x:Key="Customers" Source="{Binding Path=Customers, Mode=TwoWay}"/>
<DataTemplate x:Key="Customer">
<StackPanel Orientation="Horizontal">
<TextBox Content="{Binding Name}" />
<TextBox Content="{Binding Email}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{Binding Source={StaticResource Customers}}"
ItemTemplate="{StaticResource ResourceKey=Customer}"
Name="CustomerList"
Height="300" />
</StackPanel>
什么不工作? – ChrisF
“顾客”是一个领域还是一个财产?对于绑定工作,它必须是一个财产。 – svick
你已经给了你的'CollectionViewSource'和你的'DataTemplate'。将密钥更改为其中一个独特的密钥。 –