Δεν μπορώ να καταλάβω που κάνεις λάθος. Σε τέτοιες περιπτώσεις, καλό είναι να απομονώνεις τον κώδικα σε ένα νέο project και να δοκιμάζεις αν το functionality που προσπαθείς να πετύχεις παίζει. Μόλις το πετύχεις, αλλάζεις το βασικό project γνωρίζοντας πλέον πως θα πρέπει να συμπεριφέρεται. Δοκίμασε τα παρακάτω:
Κλάση αντί για anonymous types:
class ProdCat
{
public string CatName { get; set; }
public string ProdName { get; set; }
}
XAML:
<Window x:Class="WpfLinq.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="300">
<Window.Resources>
<CollectionViewSource x:Key="cvs" />
</Window.Resources>
<Grid>
<ListBox x:Name="listBox1"
ItemsSource="{Binding Source={StaticResource cvs}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock FontWeight="Bold"
Text="{Binding CatName}" />
<TextBlock Text="{Binding ProdName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Code behind:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DoStuff();
}
public void DoStuff()
{
NorthwindDataContext cntx = new NorthwindDataContext();
var ProdCatList = (from p in cntx.Products
from c in cntx.Categories
where p.CategoryID == c.CategoryID
select new ProdCat { CatName= c.CategoryName, ProdName = p.ProductName });
CollectionViewSource cvs = (CollectionViewSource)this.FindResource("cvs");
cvs.Source = ProdCatList;
}
}
To μόνο που χρειάζεσαι πλέον είναι ένα datacontext στη Northwind με τους πίνακες Products και Categories. Σ' εμένα το παραπάνω παίζει μια χαρά...
Vir prudens non contra ventum mingit