I have a listbox that I’m doing some databinding. As you can see I have some images associated with the data that I’m displaying… Everything is working great, except that when I change themes I need to change my image to the black images. I can’t seem to figure out how to change the images when they are bound like this.
Any ideas?
<ListBox x:Name="lbPharm" ItemsSource="{Binding col}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<TextBlock FontFamily="Segoe WP Semibold" FontWeight="Bold" FontSize="30" VerticalAlignment="Top" Margin="20,10">*</TextBlock>
<StackPanel>
<TextBlock x:Name="ItemText" Text="{Binding name}" FontSize="{StaticResource PhoneFontSizeLarge}"/>
<TextBlock x:Name="ItemNumber" Text="{Binding number}" FontSize="{StaticResource PhoneFontSizeNormal}"/>
</StackPanel>
<Image Source="Images/phone.png" Margin="20,0" x:Name="phone" Visibility="Visible">
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Tap="GestureListener_Tap_Phone"/>
</toolkit:GestureService.GestureListener>
</Image>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You should create a binding for the image source instead of setting it explicitly in XAML.
Now, just update the property in your view model as needed, as long as the class containing the property implements
INotifyPropertyChangedthe new image will show up in yourListBox.The
ImageSourceproperty may have to be aBitmapImageinstead of a string. XAML must use a converter to convert your path string when it is used as a literal but I think it doesn’t do this if you use a binding. Or you could use your own converter. Either way, construct aBitmapImageas follows:EDIT
Adding an example:
Corresponding view model:
This is code from a project I’m working on, it is similar to your case except I’m displaying the image with a
LongListSelectorinstead of aListBox. And it looks like I mislead you earlier about not being able to use a string directly for the image source, I’m doing exactly that and it works. Sorry about that.