I'm new to WPF MVVM and I've been working my way to an almost complete application. There's only one issue, and I can't seem to figure out what I'm doing wrong. Let me add that I've only got a Winforms background and Databinding is something I've never needed to do, so if the solution seems obvious, that's why.
<!-- The list of download packages. -->
<ListBox x:Name="PackagesList" DockPanel.Dock="Left" Width="120" ItemsSource="{Binding ViewRaster.RasterPackages}">
<!-- Each individual package -->
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel>
<Image Height="16" Width="16" Source="{Binding PackageImage}"/>
<TextBlock Text="{Binding PackageName}"/>
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Tag" Value="{Binding PackageDownloads}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
I've got a list of packages within the object ViewRaster as you can tell from the ItemSource of the listbox. With each package of the item source I create a listbox item that contain an image and a textblock with the name of the package, and the image of the package.
Now next, there's the "ItemContainerStyle", which I assumed would have worked the exact same way -- namely, that I could use the properties of each individual package, as was bound from the ItemsSource.
I do not seem to be able to access the individual "Package" as I would within the ItemTemplate -> DataTemplate. It's neccessary that I have the ListBoxItem have either a "Tag" or a "DataContext" set to the "PackageDownloads".
From the designer it's telling me that it's unable to find "PackageDownloads" in the Data Context of my View, but I'm not in the DataContext of my view, I'm in the DataContext of the ItemsSource.
Why is this? How can I fix this?