转换器如何null到ImageNull.jpg

时间:2011-06-07 12:30:02

标签: silverlight silverlight-4.0

这段代码不起作用? 列不显示

<sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                    <Image Tag="{Binding photo}" MaxHeight="50">
                                        <Image.Source>
                                            <BitmapImage UriSource="{Binding photo, Converter={StaticResource ConvertNullImageKey}}" />
                                        </Image.Source>
                                    </Image>
                                </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>

转换器:

public class ConvertNullImage : IValueConverter
        {

            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var image = new BitmapImage();
                try
                {
                    image = new BitmapImage(new Uri(value.ToString()));
                    return image;
                }
                catch { return new BitmapImage(new Uri("http://upload.wikimedia.org/wikipedia/commons/1/1c/No-Symbol.png")); }
            }

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

1 个答案:

答案 0 :(得分:1)

您目前正在做的是采用现有BitmapImage并尝试将另一个BitmapImage分配给其UriSource。你试过这个: -

<Image Tag="{Binding photo}" MaxHeight="50" Source="{Binding photo, Converter={StaticResource ConvertNullImageKey}}" />