0

我正在尝试根据 LongListSelector 的 DataContext 的布尔属性更改 LongListSelector 中的图像。我正在使用 ValueConverter 来实现这一点,并且代码确实到达了 ValueConverter 并返回了 BitmapImage 但这在屏幕上不可见。以下是一些相关代码:

XAML 代码(ItemTemplate 和 ValueConverter 声明):

<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>

如您所见,Image 绑定到 ValueConverter 和 isSaved 属性。

值转换器代码:

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();
    }
}

我不知道为什么不显示图像.. ValueConverter 不能返回 null。

4

1 回答 1

1

我的天,我现在肯定觉得自己很傻。这一切都很好,但我将图像文件的构建操作设置为嵌入式资源。将其更改为内容解决了问题。

于 2014-01-08T13:12:20.393 回答