【发布时间】:2012-05-09 00:58:48
【问题描述】:
有人可以在这里看到我需要更改的内容吗?我正在显示 AddressTypeClass 项目的 observablecollection。对象项而不是数据显示在列表框中。我可以在调试模式下看到对象中的数据。
XAML.CS 文件:
DataContext MyTableDataContext = new MyTableDataContext();
ObservableCollection<AddressTypeClass> theOC = new ObservableCollection<AddressTypeClass>(new MyTableDataContext().AddressTypes.AsEnumerable()
.Select(lt => new AddressTypeClass
{
AddressTypeID = lt.AddressTypeID,
AddressType = lt.AddressType,
})
.ToList());
this.listBox1.ItemsSource = theOC;
XAML 文件:
<ListBox Name="listBox1" Margin="8" Height ="200" Width ="150" FontSize="12" Foreground="#FF2F3806" ItemsSource="{Binding AddressType}" IsSynchronizedWithCurrentItem="True" >
</ListBox>
【问题讨论】:
-
您的 ItemsSource 在后面的代码中与 .xaml 中的不同。您实际上不需要在后面的代码中设置它,但 .xaml 应该设置为“theOC”。然后将 DisplayMemberPath 设置为 AddressType。
标签: c# linq xaml binding observablecollection