mainrotor
asked on
I need help adding a datacontext value to a listbox with images in it. This is a WPF application with VB.Net code-behind
Hi Experts,
I am using the code below to load thumbnail images into a listbox control in my WPF application with VB.Net code behind.
This works fine when I simply load thumbnails in my ListBox.
When I want to set the DataContext, my images don't load.
Please help me fix this problem.
Here is my current code:
CODE-BEHIND THAT WORKS WHEN I SIMPLY LOAD THUMBNAILS TO MY ListBox
CODE-BEHIND THAT DOES NOT WORK WHEN I TRY LOAD THUMBNAILS, and a DataContext value to my ListBox
NOTE: The code doesn't break, but my images stop showing
XAML CODE
I am using the code below to load thumbnail images into a listbox control in my WPF application with VB.Net code behind.
This works fine when I simply load thumbnails in my ListBox.
When I want to set the DataContext, my images don't load.
Please help me fix this problem.
Here is my current code:
CODE-BEHIND THAT WORKS WHEN I SIMPLY LOAD THUMBNAILS TO MY ListBox
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Dim ta As New ImagesDataSetTableAdapters.sp_SELECT_ImageAdapter
Dim dt As ImagesDataSet.sp_SELECT_ImageDataTable = ta.GetData(1, "20941", True)
Dim dr As DataRow
For Each dr In dt
Dim ImgSource2() As Byte = DirectCast(dr(7), Byte())
Dim stream2 As MemoryStream = New MemoryStream
stream2.Write(ImgSource2, 0, ImgSource2.Length - 1)
stream2.Seek(0, SeekOrigin.Begin)
Dim bitMap2 As New BitmapImage
bitMap2.BeginInit()
bitMap2.StreamSource = stream2
bitMap2.EndInit()
ListBox2.Items.Add(bitMap2)
Next
End Sub
CODE-BEHIND THAT DOES NOT WORK WHEN I TRY LOAD THUMBNAILS, and a DataContext value to my ListBox
NOTE: The code doesn't break, but my images stop showing
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Dim ta As New ImagesDataSetTableAdapters.sp_SELECT_ImageTableAdapter
Dim dt As ImagesDataSet.sp_SELECT_ImageDataTable = ta.GetData(1, "20941", True)
Dim dr As DataRow
For Each dr In dt
Dim ImgSource2() As Byte = DirectCast(dr(7), Byte())
Dim stream2 As MemoryStream = New MemoryStream
stream2.Write(ImgSource2, 0, ImgSource2.Length - 1)
stream2.Seek(0, SeekOrigin.Begin)
Dim bitMap2 As New BitmapImage
bitMap2.BeginInit()
bitMap2.StreamSource = stream2
bitMap2.EndInit()
Dim lbItem2 As New ListBoxItem
lbItem2.Content = bitMap2
lbItem2.DataContext = dr(0).ToString
ListBox2.Items.Add(lbItem2)
Next
End Sub
XAML CODE
<Window.Resources>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="4"
CornerRadius="5" Margin="6"
>
<Image
Source="{Binding}"
Stretch="Fill"
Width="100" Height="120"
/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid Margin="15">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="60" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox x:Name="ListBox2" Grid.ColumnSpan="1" Grid.Column="4" Grid.Row="5" ItemsSource="{Binding}" />
</Grid>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.