您没有将“集合”设置为 ItemsSource...因此您没有任何可供选择的内容。这将选择列表CustomerName
.
<ListBox ItemsSource="{Binding Source={StaticResource Cust}, Path=CustomerName}"
Height="172" HorizontalAlignment="Left" Margin="27,23,0,0" Name="lstStates"
VerticalAlignment="Top" Width="245" />
但实际上,你应该重组你的CustomerList
类...不需要有单独的列表来复制Name
and LastName
的领域Customer
- 这意味着您不必要地重复数据,并且数据也可能与每个集合不同步。
您还应该考虑使用 INotifyPropertyChanged,并在您的类上使用 INotifyCollectionChanged(例如,使用ObservableCollection
代替List
,并实施INotifyPropertyChanged
on the Customer
类)如果可以更改集合或数据。
e.g.
public class CustomerList
{
public ObservableCollection<Customer> Customers { get; set; }
public CustomerList()
{
Customers = new ObservableCollection<Customer>();
Customers.Add(new Customer { Name = "Name1", LastName = "LastName1" });
Customers.Add(new Customer { Name = "Name2", LastName = "LastName2" });
}
}
<ListBox ItemsSource="{Binding Source={StaticResource Cust}, Path=Customers}" DisplayMemberPath="Name" Height="172" HorizontalAlignment="Left" Margin="27,23,0,0" Name="lstStates" VerticalAlignment="Top" Width="245" />