使用c#在wpf listview中添加图像

时间:2013-10-03 08:45:04

标签: c# wpf image listview

我是c#和WPF的新用户,我在使用文字和图片填充ListView时遇到问题。

这是我的wpf代码:

  <Grid>
    <ListView Name="MyList" Margin="0,0,328.4,-0.2" >
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Rete" DisplayMemberBinding="{Binding Rete}"/>
                <GridViewColumn Header="Immagine" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Image  Source="{Binding Immagine}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
        <Image Height="100" Width="100"/>
    </ListView>
    <Button Content="Button" HorizontalAlignment="Left" Margin="324,83,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
     </Grid>

这是我的c#代码:

while (r.Read())
        {

            MyList.Items.Add(new { Rete = r.GetString(0), Immagine = r.GetString(1) });
        }

感谢所有我已经解决了!!!我改变了数据库中的图像路径代码是正确的!! :)

1 个答案:

答案 0 :(得分:3)

这是我的xaml代码。

  <Grid>
    <ListView x:Name="ListView1"   VirtualizingStackPanel.IsVirtualizing="True" Height="200"  ItemsSource="{Binding ListViewItemsCollections}">
        <ListView.View>
            <GridView AllowsColumnReorder="False">
                <GridViewColumn x:Name="GridViewColumnName" Header="Name"  Width="200">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image x:Name="Image_GridViewColumnName" Width="100" Height="50" Source="{Binding GridViewColumnName_ImageSource}" />
                                <Label Content="{Binding GridViewColumnName_LabelContent}" Width="50" Height="100"  />
                                <Label Content="{Binding GridViewColumnName_ID}" Visibility="Hidden" />
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn x:Name="GridViewColumnTags" Header="Tags" Width="100" DisplayMemberBinding="{Binding GridViewColumnTags}" />
                <GridViewColumn x:Name="GridViewColumnLocation" Header="Location" Width="238" DisplayMemberBinding="{Binding GridViewColumnLocation}" />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

这是我的c#部分..

 public ObservableCollection<ListViewItemsData> ListViewItemsCollections { get { return _ListViewItemsCollections; } }
    ObservableCollection<ListViewItemsData> _ListViewItemsCollections = new ObservableCollection<ListViewItemsData>();

    public MainWindow()
    {

        InitializeComponent();

        ListViewItemsCollections.Add(new ListViewItemsData()
        {
            GridViewColumnName_ImageSource = @"D:\rd\C Sharp\general\StackOverFlowAnswers\WPF\MSD.JPG",
            GridViewColumnName_LabelContent = "shanmugharaj"
        });

        ListView1.ItemsSource = ListViewItemsCollections;
    }

    public class ListViewItemsData
    {
        public string GridViewColumnName_ImageSource { get; set; }
        public string GridViewColumnName_LabelContent { get; set; }
        public string GridViewColumnName_ID { get; set; }
        public string GridViewColumnTags { get; set; }
        public string GridViewColumnLocation { get; set; }
    }
}

我测试了这些工作正常.. 如果我的理解是正确的,那么你需要......