2012-04-28 48 views
0

有人能看到我需要改變嗎?我正在顯示AddressTypeClass項目的observable集合。對象項顯示在列表框中而不是數據中。我可以在調試模式下看到對象中的數據。Linq的ObservableCollection

的XAML.CS FILE:

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> 
+0

您的ItemsSource在代碼背後不同於.xaml。你並不需要在後面的代碼中設置它,但是.xaml應該被設置爲「theOC」。然後將DisplayMemberPath設置爲AddressType。 – 2012-04-28 17:14:34

回答

0

你需要一個ItemTemplate添加到您的列表框,例如

<ListBox Name="listBox1" Margin="8" Height ="200" Width ="150" FontSize="12" Foreground="#FF2F3806" ItemsSource="{Binding AddressType}" IsSynchronizedWithCurrentItem="True" > 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel> 
     <TextBlock Text="{Binding Path=AddressType}" /> 
     </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
相關問題