4

I am a new user to c# and WPF and I have a problem to populate a ListView with text and image.

This is my wpf code:

  <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>

and this is my c# code:

while (r.Read())
        {

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

thanks to all i have solved!!! i changed the image path in the databse the code was correct!! :)

8
  • Where is your problem? Reading data or displaying the data? Commented Oct 3, 2013 at 8:58
  • My problem is displaying data! the text is ok but i don't see the image. The image is in the debug folder and i read her name correctly! Commented Oct 3, 2013 at 9:03
  • Why on earth would you put an image in the Debug folder? It should be in a folder named Images for clarity surely? Also, please show us the value(s) that you are using for the Image.Source in your Immagine property. Commented Oct 3, 2013 at 9:06
  • First, like @Sheridan said it shouldn't be in the Debug folder. you should have it in some images folder in your project hirarchy. Then make sure Immagine contains the absolute or relative path of the image, for example "images\myimage.jpg" and make sure that path is accessible from the executing code. Commented Oct 3, 2013 at 9:09
  • @Stefano on a side note, since you are new to C# and WPF I'd suggest you to learn MVVM and not add items to the list directly. Commented Oct 3, 2013 at 9:11

1 Answer 1

6

This is my xaml code.

  <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>

This is my c# part..

 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; }
    }
}

I tested with these its working fine.. If my understanding id right this is you need..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.