无法在列表视图中显示图片

Can't display picture in listview

您好,我在列表视图中显示图片时遇到问题。我有一个 url 的数组,我必须从中下载图片,但出于测试目的,我尝试用本地硬盘驱动器中的相同图片填充每一行。我可以绑定数据,但到目前为止,我唯一可以打印到图片列的是对象的名称,而不是图片本身 (DisplayData.Image) 这是我的 xaml 和 c# 代码:

XAML

   <Window x:Class="TestIT.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestIT"
    Title="MainWindow" Height="510.9" Width="723">
<Window.Resources>
    <local:InterfaceBinding x:Key="adjust" />
</Window.Resources>
<Grid>
    <Grid HorizontalAlignment="Stretch" Height="100" VerticalAlignment="Stretch" Width="517" Margin="98,10,100,370" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>


        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>

        <Label x:Name="labelNewsTop" Content="Top News" Grid.Row="0" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center" Grid.Column="0" MouseLeftButtonUp="labelNewsTop_MouseLeftButtonUp" />
        <Label x:Name="labelNewsBusiness" Content="Business" Grid.Row="0" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center" Grid.Column="1" />
        <Label x:Name="labelNewsTechnology" Content="Technology" Grid.Row="0" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center" Grid.Column="2" />
        <Label x:Name="labelNewsEntertainment" Content="Entertainment" Grid.Row="0" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center" Grid.Column="3" />
        <Label x:Name="labelNewsScience" Content="Sport" Grid.Row="0" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center" Grid.Column="4" />
        <Label x:Name="labelNewsHealth" Content="Health" Grid.Row="0" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center" Grid.Column="5" />
        <Image x:Name="iamgetest" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="100" Margin="24,68,0,-68" VerticalAlignment="Top" Width="100" Grid.Column="4"/>

    </Grid>
    <ListView x:Name="testView" HorizontalAlignment="Left" Height="304" Margin="0,105,0,0" VerticalAlignment="Top" Width="715">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"/>
                <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=Value}"/>
                <GridViewColumn Header="Lele" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Image  Height="50" Width="50" Stretch="Fill" Source="{Binding Path=Image}"></Image>

                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

</Grid>
</Window>

C#

        GNews gnews = new GNews();
        List<NewsModel> bla = gnews.getTopNews();


        for (int i = 0; i < bla.Count - 1; i++)
        {


            DisplayData data = new DisplayData();
            data.Name = bla[i].title;
            data.Value = bla[i].url;
            ImageSource mySource = new BitmapImage(new Uri("D:\New folder\New folder\other\Wall\010.jpg"));
            data.Image = new Image();
            data.Image.Source = mySource;

            this.testView.Items.Add(data);

        }
    }
}

public class DisplayData
{
    public string Name { get; set; }
    public string Value { get; set; }

    public Image Image { get; set; }

}

您应该绑定 Uri 而不是图像。

public class DisplayData
{
   public string Name { get; set; }
   public string Value { get; set; }

   public Uri Image { get; set; }
}

你的循环看起来像

for (int i = 0; i < bla.Count - 1; i++)
    {


        DisplayData data = new DisplayData();
        data.Name = bla[i].title;
        data.Value = bla[i].url;

        data.Image = new Uri("D:\New folder\New folder\other\Wall\010.jpg");


        this.testView.Items.Add(data);

    }