none
how to add images in Listview (images from file but database related) RRS feed

  • Question

  • more questions :)

    I want to add images to Listview, the question is the listviewitems of this listview is uncertained (read from datebase), so I can't do this:

    <ListViewItem Background="LightSlateGray" Foreground="Orange"

                 FontFamily="Georgia" FontSize="14" FontWeight="Bold">

         <StackPanel Orientation="Horizontal">

                <Image Source="MangoShake.jpg" Height="30"></Image>

                <TextBlock Text="Mango Shake"></TextBlock>

         </StackPanel>

    </ListViewItem>

     

    I have to do it in C# file not in XAML

    So, how to put image(from file) into listview in C#

    Winform I can use imagelist,but wpf can't

    Sorry for poor my English.... thank you!

    Friday, October 8, 2010 2:33 AM

Answers

  • Hello,  You can do it like that.

    ListViewItem myItem = new ListViewItem();
    Image img = new Image();
    img.Source = new BitmapImage(new Uri(...));
    myItem.Content = img; // Set Content, You might also need to set some others property of Image to make it display correctly.

    ListView1.add(myItem);


    Keep imporving.
    Yiling Lai. MVP (Visual C++ and Client App Dev)
    • Marked as answer by vpoint2010 Friday, October 8, 2010 7:23 AM
    Friday, October 8, 2010 3:25 AM

All replies

  • Hello,

    How about you give the Image element x:Name="img", then we can access the image in code-behind like member variable. Then update image source dynamically.

    img.Source = new BitmapImage(new Uri(...));

    If you ListView is controlled with DataTemplate, then please refer to this.

    http://blogs.msdn.com/b/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx

     

    Hope this helps.


    Keep imporving.
    Yiling Lai. MVP (Visual C++ and Client App Dev)
    Friday, October 8, 2010 2:47 AM
  • Hello,

    How about you give the Image element x:Name="img", then we can access the image in code-behind like member variable. Then update image source dynamically.

    img.Source = new BitmapImage(new Uri(...));

    If you ListView is controlled with DataTemplate, then please refer to this.

    http://blogs.msdn.com/b/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx

     

    Hope this helps.


    Keep imporving.
    Yiling Lai. MVP (Visual C++ and Client App Dev)


    thanks again :)

    String mysql = "select * from record where time >= '" + ss + "' and time < '" + sl + "' order by time";
          DataSet ds = new DataSet();
          try
          {
            ds = myConn.Search(mysql, "sql");
            int dlen = ds.Tables[0].Rows.Count;
                    while (dlen > 0)
            {
              dlen--;
              int dtlen = ds.Tables[0].Rows[dlen].ItemArray[1].ToString().Length;
              String dtime = ds.Tables[0].Rows[dlen].ItemArray[1].ToString().Substring(9, dtlen - 9);
              string str = dtime + " " +
                ds.Tables[0].Rows[dlen].ItemArray[4].ToString() + " " +
                ds.Tables[0].Rows[dlen].ItemArray[3].ToString() + " " +
                ds.Tables[0].Rows[dlen].ItemArray[2].ToString() + " " + ds.Tables[0].Rows[dlen].ItemArray[0].ToString();
    
              ListViewItem myItem = new ListViewItem();
              myItem.Height = 26;
    
              myItem.Content = str;
    
              Listview1.Items.Add(myItem);
            }
          }
          catch { }
    

     I am sorry, I can use a Image as a member variable, but how to put the  Image into the Listview? there is no property or method in ListViewItem(or ListView) can do this ...

    The code I attached is sample, can you show me where and how to put the image member variable into the ListView?

    I really wish I can do this :

    ListViewItem myItem = new ListViewItem();
    Image img = new Image();
    img.Source = new BitmapImage(new Uri(...));
    myItem.Image = img;
    ListView1.add(myItem);
    
    //all I have to do is choose picture for different ListViewItems...
    //but it is impossible.... 冏
    


      

     

    Friday, October 8, 2010 3:05 AM
  • Hello,  You can do it like that.

    ListViewItem myItem = new ListViewItem();
    Image img = new Image();
    img.Source = new BitmapImage(new Uri(...));
    myItem.Content = img; // Set Content, You might also need to set some others property of Image to make it display correctly.

    ListView1.add(myItem);


    Keep imporving.
    Yiling Lai. MVP (Visual C++ and Client App Dev)
    • Marked as answer by vpoint2010 Friday, October 8, 2010 7:23 AM
    Friday, October 8, 2010 3:25 AM
  • Hello,  You can do it like that.

    ListViewItem myItem = new ListViewItem();
    Image img = new Image();
    img.Source = new BitmapImage(new Uri(...));
    myItem.Content = img; // Set Content, You might also need to set some others property of Image to make it display correctly.

    ListView1.add(myItem);


    Keep imporving.
    Yiling Lai. MVP (Visual C++ and Client App Dev)

    Bravo! It works! thank you again:)


                        Image img = new Image();
                        img.Source = new BitmapImage(new Uri(@"d:\1.bmp"));
                        WrapPanel wrap = new WrapPanel();
                        Label label = new Label();
                        label.Content =str;
                        wrap.Children.Add(img);
                        wrap.Children.Add(label);
                        myItem.Content = wrap;

    Friday, October 8, 2010 3:49 AM