Hello everyone,
I tried to look around to see if I am missing something basic, but I can't seem to understand what I am doing wrong.
I have a listbox where I would like to bind the itemssource to a collectionview of photos from a dataset. The problem is, I need to take the directory where the images exist from the MasterView CollectionView, and the individual filenames from the DetailView CollectionView. So I defined the resources:
<CollectionViewSource x:Key="MasterView" />
<CollectionViewSource Source="{Binding Source={StaticResource MasterView}, Path='tblGallery_tblPictures'}" x:Key="DetailView" />
This seems to work without a problem, I can navigate through the data set and see the Master and Detail data.
To get the full image path, I figured out that I needed a multibinding to put the two together, and have sucessfully created one:
<ListBox HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding Source={StaticResource DetailView}}">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Width="120"
Stretch="Uniform"
Margin="8,8,8,8">
<Image.Source>
<BitmapImage DecodePixelWidth="120">
<BitmapImage.UriSource>
<MultiBinding Converter="{StaticResource imagePathConverter}">
<Binding Source="{StaticResource MasterView}" Path="galleryLocation" />
<Binding Path="pictureFileName" />
</MultiBinding>
</BitmapImage.UriSource>
</BitmapImage>
</Image.Source>
</Image>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" HorizontalAlignment="Stretch" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
If values.Count() <> 2 Then
Throw New InvalidOperationException("Correct Values Not Provided")
End If
Dim directory As String
Dim fileName As String
Dim imgUri As Uri = Nothing
If TypeOf values(0) Is String And TypeOf values(1) Is String Then
directory = CType(values(0), String)
fileName = CType(values(1), String)
If directory <> "" And fileName <> "" Then
If directory.LastIndexOf("/") = directory.Length - 1 Then
imgUri = New Uri(directory & fileName, UriKind.Absolute)
Else
imgUri = New Uri(directory & "/" & fileName, UriKind.Absolute)
End If
End If
End If
Return (imgUri)
End Function
My assumption with the proceeding code, is that the MultiBinding that is inside the DataTemplate has access to the BindingSource that is defined as the ItemsSource from the listbox. However when I look into the MultiValueConverter code it only passes the data from the MasterView and a generic object for the second value.
To see if I could get it to work, I added the detail source information in the multibinding:
<MultiBinding Converter="{StaticResource imagePathConverter}">
<Binding Source="{StaticResource MasterView}" Path="galleryLocation" />
<Binding Source="{StaticResource DetailView}" Path="pictureFileName" />
</MultiBinding>
That at least enabled me to see that there were 5 pictures in the listbox, but they were all the same picture. It seems that the multibinding access to the DetailView CollectionView only showed the first item.
Is there a way to link the "pictureFileName" to the current ItemsSource item so that the right picture can be displayed?
thanks!
Josh