简体   繁体   中英

WPF - Load Image in ListView

i have a little problem with my WPF-Application.

Well, i have a ListView which shows at the first column a image. Each element can or cannot have a image, so from time to time i load a "default" image in this column.

Here is the problem.

When i load the default image, there is no image shown in the listview.

Here is the codepart, which adds the default image:

 Uri src = new Uri(@"/myapp;component/Images/Picture.png", UriKind.RelativeOrAbsolute);
                        BitmapImage small_image_bmp = new BitmapImage();

                        small_image_bmp.BeginInit();
                        small_image_bmp.CacheOption = BitmapCacheOption.OnLoad;
                        small_image_bmp.UriSource = src;
                        small_image_bmp.EndInit();
                        small_image_bmp.Freeze();

                        dto.Bild1_Bitmap = small_image_bmp;               


                    this.liste.Add(dto);

This code is not working.

But this part works very well :

 Uri src = new Uri(@"C:\Users\me\Documents\Visual Studio 2010\Projects\myapp\myapp\Images\Picture.png", UriKind.RelativeOrAbsolute);
                        BitmapImage small_image_bmp = new BitmapImage();

                        small_image_bmp.BeginInit();
                        small_image_bmp.CacheOption = BitmapCacheOption.OnLoad;
                        small_image_bmp.UriSource = src;
                        small_image_bmp.EndInit();
                        small_image_bmp.Freeze();

                        dto.Bild1_Bitmap = small_image_bmp;               


                    this.liste.Add(dto);

As you can see, the part who isnt working has a relative path and the code, who is working, has a absolute path.

But of course i cant use the absolute path...

Here is the XAML of the ListViewColumn:

  <GridViewColumn Header="Bild">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Image Width="100" Height="100" Source="{Binding Bild1_Bitmap}"/>
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

If code you posted for loading an image is in same assembly as of image. You can ignore component path and use like this - Images\\Picture.png .

Also make sure image build action is set to Resource .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM