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?

2 Answers 2

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" } )
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your first solution display System.windows.controls.Image text and your Second solution does not compile. It does not support <image> tag inside <gridViewColumn>
@MdKamruzzamanPallob You need to wrap the <Image> tab in a <GridViewTemplateColumn.CellTemplate> and <DataTemplate> tags. See this answer for an example.
1

Try a CellTemplate

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

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.