1
votes

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?

1
Do not explicitly set the DataContext of a ListBoxItem (or any other item container of an ItemsControl). This is already done automatically by the framework. In other words, remove the entire ItemContainerStyle.Clemens
Do not set Tag. Just make sure that PackageImage and PackageName are public properties of the item class, i.e. the element type of the RasterPackages collection.Clemens
How would I then retrieve the individual package from any given ListBox item in another part of my application?Proton Eddie
ListView Grid.Column="1" ItemsSource="{Binding SelectedItem.Tag, ElementName=PackagesList}">Proton Eddie
Is how I'm currently doing it, and this seems rather hacky.Proton Eddie

1 Answers

0
votes

You don't need to explicitly set a ListBoxItems's DataContext or its Tag property to get access to the selected item from the RasterPackages collection.

The selected RasterPackage object is directly accessible via the ListBox's SelectedItem property:

<ListView
   ItemsSource="{Binding ElementName=PackagesList, Path=SelectedItem.PackageDownloads}">