1

I am facing problem in adding image to a column in ListView in wpf. I have two column say x and y and i want to add an image in y column. I have tried a lot. My xaml is below---

 `<ListView Name="listView1" ItemsSource="{Binding}" DataContext="{Binding}" SelectionMode="Single">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Directory" DisplayMemberBinding="{Binding Directory}" />
                <GridViewColumn Header="Status" DisplayMemberBinding="{Binding Status}" >                        
               </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
 `

and my c# code is below

       public class FolderPath
    {
        public string Directory { get; set; }

        public System.Drawing.Image Status { get; set; }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {                    
 ObservableCollection<FolderPath> _FolderCollection = new ObservableCollection<FolderPath>();
 BitmapImage b = new BitmapImage();
 b.BeginInit();
 b.UriSource = new Uri(@"C:\Users\smk\Documents\Visual Studio 2010\Projects\Folder_locker\Folder_locker\folder_lock.ico");
 b.EndInit();

 System.Windows.Controls.Image i= new System.Windows.Controls.Image();
 i.Height = 20;
 i.Source = b;

 System.Drawing.Image.FromFile(@"C:\Users\smk\Documents\Visual Studio 2010\Projects\Folder_locker\Folder_locker\folder_lock.ico");


      listView1.Items.Add(new FolderPath { Directory = "something", Status = System.Drawing.Image.FromFile(@"C:\Users\smk\Documents\Visual Studio 2010\Projects\Folder_locker\Folder_locker\folder_lock.ico") });                  
    }

it displays something in first column but displays nothing in 2nd column. I have tried both system.windows.controls.image and system.drawing.image both but it does not show image. what can I do?

Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38

2 Answers2

1

Try a CellTemplate

http://msdn.microsoft.com/en-us/library/system.windows.controls.gridviewcolumn.celltemplate.aspx

paparazzo
  • 44,497
  • 23
  • 105
  • 176
1

Try this:

public class FolderPath
{
    public string Directory { get; set; }
    public string Status { get; set; }
}

<GridViewColumn Header="Status">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding Path=Status}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>


private void button1_Click(object sender, RoutedEventArgs e)
{                    
    ObservableCollection<FolderPath> _FolderCollection = new ObservableCollection<FolderPath>();

    listView1.Items.Add(new FolderPath { Directory = "something", Status = @"C:\Users\smk\Documents\Visual Studio 2010\Projects\Folder_locker\Folder_locker\folder_lock.ico" } )
}
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
  • Your first solution display System.windows.controls.Image text and your Second solution does not compile. It does not support tag inside – Md Kamruzzaman Sarker Feb 27 '12 at 19:01
  • @MdKamruzzamanPallob You need to wrap the `` tab in a `` and `` tags. See [this answer](http://stackoverflow.com/a/4725474/302677) for an example. – Rachel Feb 27 '12 at 19:23