Question

I'm trying to change an Image in a LongListSelector on basis of a boolean property of the DataContext of the LongListSelector. I'm using a ValueConverter in order to achieve this, and the code does reach the ValueConverter and returns a BitmapImage but this isn't visible on screen. Here's some of the relevant code:

XAML code (ItemTemplate and the ValueConverter declaration):

<local:BoolToImage x:Key="BoolImageConverter"/>

DataTemplate x:Key="itemTemplate">
        <Grid Margin="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>

            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Foreground="Black" Margin="0" FontFamily="Segoe WP Light" FontSize="29.333" VerticalAlignment="Center" Text="{Binding BeginTijdTimeOnly}" />
            <TextBlock Grid.Column="1" TextWrapping="Wrap" Foreground="Black" Margin="0" FontFamily="/LimburgsLeed;component/Fonts/Fonts.zip#Champion" FontSize="48" VerticalAlignment="Center" Text="{Binding Artiest.Naam}" />
            <Image x:Name="image" Grid.Column="2" Source="{Binding Path=isSaved, Converter={StaticResource BoolImageConverter}}" VerticalAlignment="Center" Margin="0, 0, -1, 0" MouseLeftButtonDown="fav_Click"/>
        </Grid>
    </DataTemplate>

As you can see the Image is bound to the ValueConverter and the isSaved property.

ValueConverter code:

public class BoolToImage : IValueConverter
{
    public BitmapImage TrueImage = new BitmapImage();
    public BitmapImage FalseImage = new BitmapImage();

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        TrueImage.UriSource = new Uri("/Images/ThumbSelected@2x.png", UriKind.RelativeOrAbsolute);
        FalseImage.UriSource = new Uri("/Images/thumb.png", UriKind.RelativeOrAbsolute);

        if (!(value is bool))
        {
            return this.FalseImage;
        }

        bool b = (bool)value;
        if (b)
        {
            return this.TrueImage;
        }
        else
        {
            return this.FalseImage;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I don't have a clue why the image isn't shown.. The ValueConverter CAN'T return null.

Was it helpful?

Solution

My god, I sure feel stupid now. This all works fine, but I set the Image files their Build Action to Embedded Resource. Changing it to content resolved the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top