Results 1 to 15 of 15

Thread: [RESOLVED] WPF: Render an Image in a ListView

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Resolved [RESOLVED] WPF: Render an Image in a ListView

    I am trying to bind an image in a listview control. What I have got working so far has been copied from the tutorial in the Utility Bank section of this site.

    Is there way to add an Image in this list? How would I go about binding an image to this listview?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: WPF: Render an Image in a ListView

    Put a background image in the listview or show an image per row of the listview?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: WPF: Render an Image in a ListView

    This is what I have currently.

    Code:
                    <GridView>
                        <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
                        <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Surname}"/>
                        <GridViewColumn Header="Date Of Birth" DisplayMemberBinding="{Binding DateOfBirth}"/>
                        <GridViewColumn Header="Photo" DisplayMemberBinding="{Binding IdPic}"/>
                        <GridViewColumn x:Name="Image">
                            <Image Source="{Binding Path=/}" Height="75" Width="75"></Image>
                        </GridViewColumn>
                    </GridView>
    How do I display an image from a file in the grid?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: WPF: Render an Image in a ListView

    You have to make changes to the CellTemplate in order to display an image.
    Code:
    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" />
    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" />
    <GridViewColumn Width="100">
        <GridViewColumn.CellTemplate>
            <DataTemplate>
                <Border CornerRadius="2,2,2,2" Width="40" Height="40" Background="#FFFFC934" BorderBrush="#FF000000" Margin="3,3,3,3">
                    <Image HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Height="Auto" Source="{Binding Path=ImagePath}" Stretch="Fill" Margin="2,2,2,2"/>
                </Border>
            </DataTemplate>
        </GridViewColumn.CellTemplate>
    </GridViewColumn>
    Here's how I specified the images.
    Code:
    _personCollection.Add(new Person("FirstName1", "LastName1",@"C:\image1.bmp"));
    _personCollection.Add(new Person("FirstName2", "LastName2",@"C:\image2.bmp"));
    And here is the class:
    Code:
    namespace WpfApplication1
    {
        public class Person
        {
            public Person(string firstName, string lastName, string imagePath)
            {
                FirstName = firstName;
                LastName = lastName;
                ImagePath = imagePath;
            }
    
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string ImagePath { get; set; }
        }
    }
    Its all in C# but it can easily converted to VB.Net.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: WPF: Render an Image in a ListView

    dee-u,
    I noticed that you're using a string for the imagepath. If you were to substitute this with a bitmap instead of a string, how would you do it?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: WPF: Render an Image in a ListView

    Where are you going to get the bitmap?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: WPF: Render an Image in a ListView

    Quote Originally Posted by dee-u View Post
    Where are you going to get the bitmap?
    Right now, I plan to go to the file system. Later it will be fetched from a database.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: WPF: Render an Image in a ListView

    Ok, here are the changes I made.

    Template:
    Code:
    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="316" Width="440" Loaded="Window_Loaded">
        <Grid>
            <ListView Margin="24,46,46,77" Name="listView1">
                <ListView.View>
                    <GridView>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" />
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" />
                    <GridViewColumn Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Border CornerRadius="2,2,2,2" Width="40" Height="40" Background="#FFFFC934" BorderBrush="#FF000000" Margin="3,3,3,3">
                                    <Image HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Height="Auto" Source="{Binding Image}" Stretch="Fill" Margin="2,2,2,2"/>
                                </Border>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </Window>

    Sample usage:
    Code:
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        BitmapImage image1 = new BitmapImage(new Uri(@"C:\PIS\rmr.bmp"));
        BitmapImage image2 = new BitmapImage(new Uri(@"C:\PIS\ping.jpg"));
    
        //_person.Add(new Person("FirstName1", "LastName1",@"C:\image1.bmp"));
        //_person.Add(new Person("FirstName2", "LastName2",@"C:\image2.bmp"));
    
        _person.Add(new Person("FirstName1", "LastName1", image1));
        _person.Add(new Person("FirstName2", "LastName2", image2));
        listView1.ItemsSource = personCollection;
    }
    class:
    Code:
    namespace WpfApplication1
    {
        public class Person
        {
            public Person(string firstName, string lastName, BitmapImage image)
            {
                FirstName = firstName;
                LastName = lastName;
                Image = image;
            }
    
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public BitmapImage Image { get; set; }
        }
    }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: WPF: Render an Image in a ListView

    The above code does not display an image for me. There's no error, but the image displayed in the grid is a blank. I have attached a screen shot.

    Ignore the image in the lower left corner of the frame.
    Attached Images Attached Images  
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  10. #10

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: WPF: Render an Image in a ListView

    Here's my code. It is in VB.NET
    vb.net Code:
    1. Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    2.         Dim image1 As New BitmapImage(New Uri("d:\user\Supes.jpg"))
    3.         Dim image2 As New BitmapImage(New Uri("d:\user\ab_face.jpg"))
    4.  
    5.         'xImage = New BitmapImage
    6.         'xImage.BeginInit()
    7.         'xImage.UriSource = New Uri("d:\user\Supes.jpg", UriKind.RelativeOrAbsolute)
    8.         'xImage.EndInit()
    9.         'imgPic.Source = xImage
    10.  
    11.         _EmployeeCollection.Add(New Employee("Bruce", "Wayne", "25-DEC-1900", image1))
    12.         _EmployeeCollection.Add(New Employee("Clark", "Kent", "15-JAN-1901", image2))
    13.     End Sub
    14.   Private Sub OnButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    15.         ListView1.ItemsSource = _EmployeeCollection
    16.     End Sub
    17.  
    18.     Public ReadOnly Property EmployeeCollection() As ObservableCollection(Of Employee)
    19.         Get
    20.             Return _EmployeeCollection
    21.         End Get
    22.     End Property

    vb.net Code:
    1. Imports System.Collections.ObjectModel
    2. Public Class Employee
    3.     Private _FirstName As String
    4.     Private _Surname As String
    5.     Private _DateOfBirth As Date
    6.     Private _IDPic As BitmapImage
    7.     Public ReadOnly Property IdPic() As BitmapImage
    8.         Get
    9.             Return _IDPic
    10.         End Get
    11.     End Property
    12.  
    13.     Public ReadOnly Property FirstName() As String
    14.         Get
    15.             Return _FirstName
    16.         End Get
    17.     End Property
    18.     Public ReadOnly Property DateOfBirth() As Date
    19.         Get
    20.             Return _DateOfBirth
    21.         End Get
    22.     End Property
    23.     Public ReadOnly Property Surname() As String
    24.         Get
    25.             Return _Surname
    26.         End Get
    27.     End Property
    28.  
    29.     Public Sub New(ByVal FN As String, ByVal SN As String, ByVal DOB As Date, ByVal IdPic As BitmapImage)
    30.         _FirstName = FN
    31.         _Surname = SN
    32.         _DateOfBirth = DOB
    33.         _IDPic = IdPic
    34.     End Sub
    35. End Class
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: WPF: Render an Image in a ListView

    Did you change the template?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  12. #12

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: WPF: Render an Image in a ListView

    I have changed my XAML code. This is how it looks.

    Code:
        <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Learning Curve" Height="350" Width="525">
        <Grid>
            <Button Content="Click Me" Click="OnButtonClick" Margin="393,262,12,12">
                <Button.Background>
                    <LinearGradientBrush>
                        <GradientStop Color="Yellow" Offset="0" />
                        <GradientStop Color="Green" Offset="1" />
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <ListView Margin="15,38,12,56" Name="ListView1" Background="#FF90865C" Foreground="Black">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
                        <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Surname}"/>
                        <GridViewColumn Header="Date Of Birth" DisplayMemberBinding="{Binding DateOfBirth}"/>
                        <GridViewColumn Header="Photo" Width="100">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Border CornerRadius="2,2,2,2" Width="40" Height="40" Background="#FFFFC934" BorderBrush="#FF000000" Margin="3,3,3,3">
                                        <Image HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Height="Auto" Source="{Binding Image}" Stretch="Fill" Margin="2,2,2,2"/>
                                    </Border>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
            <Image Height="34" HorizontalAlignment="Left" Margin="33,269,0,0" Name="imgPic" Stretch="Fill" VerticalAlignment="Top" Width="42" Source="/DataBrowser;component/Images/smoking_av.jpg" />
        </Grid>
     </Window>
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  13. #13
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: WPF: Render an Image in a ListView

    Instead of this
    Code:
    Source="{Binding Image}"
    you should pass the property where you are storing the BitmapImage.
    Code:
    Source="{Binding IdPic}"
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  14. #14

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: WPF: Render an Image in a ListView

    Thanks. That worked.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  15. #15
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [RESOLVED] WPF: Render an Image in a ListView

    No problem.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width