开发者

How set default image?

开发者 https://www.devze.com 2023-03-08 02:24 出处:网络
DataGrid: <sdk:DataGridTemplateColumn> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate>

DataGrid:

<sdk:DataGridTemplateColumn>
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Image Tag="{Binding photo}" MinWid开发者_开发百科th="50"  Source="{Binding photo, Converter={StaticResource ConvertNullImageKey}}"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>

Converter if value not uri return iamge from ImageResource.noimage. But this bitmap...How return URI on bitmap?

public class ConvertNullImage : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                try
                {
                    Uri uri = new Uri(value.ToString(), UriKind.Relative);
                    return uri;
                }
                catch { return new Uri(ImageResource.noimage); }
            }

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


The Image.Source property is not of type Uri, it is of type ImageSource, see MSDN:

http://msdn.microsoft.com/en-us/library/system.windows.controls.image.source%28VS.95%29.aspx

When you set an Image.Source in XAML, the XAML parser is cunningly converting your URI into an ImageSource.

So - you need to create a BitmapImage in your value converter. See this related question:

Image UriSource and Data Binding


The Source property of the Image expects an ImageSource, and not a URI. This works in xaml thanks to a TypeConverter. You can create a BitmapImage from the path and return that as shown here.

If I understand correctly, the second part of the problem is that you want to bind (as a default value) to an image from a Resource file. If that's so, here's an article on how to do that on MSDN

hope this helps :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号