Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3803486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:22:14+00:00 2026-05-19T14:22:14+00:00

I have a very simple question. I have a ListView control and I want

  • 0

I have a very simple question.

I have a ListView control and I want to know how to create an item with an icon on the left side. The items will be dynamically added in code in C# and not through XAML.

Image Sample: here

Something similar to above (excluding the Manage Records header). I managed to do the one above by creating grids dynamically (not using a ListView control) but I’m not sure on how to control the events (click, etc).

Thanks in advance. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:22:15+00:00Added an answer on May 19, 2026 at 2:22 pm

    Solution consists in overriding view item DataTemplate.

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        xmlns:self="clr-namespace:WpfApplication1"
        xmlns:props="clr-namespace:WpfApplication1.Properties">
    <Window.Resources>
        <self:ImageConverter x:Key="Conv"/>
    
        <DataTemplate x:Key="Template">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Path=Icon, Converter={StaticResource Conv}}"
                       Width="64"
                       Height="64"/>
                <TextBlock Text="{Binding Name}"
                           VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    
    </Window.Resources>
    <StackPanel>
        <ListView ItemsSource="{Binding Items}" 
                  ItemTemplate="{StaticResource Template}"/>
    </StackPanel>
    

    Then we have to set our PresentationModel as view’s DataContext in code behind this view:

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new SampleModel();
        }
    

    As you can see from binding expression in XAML our presentation model should expose Items property (if you consider changing Items list in runtime, underlying collection must be ObservableCollection in order to ListView reacts on its changes):

    public class SampleModel 
    {
        public IEnumerable<ViewData> Items
        {
            get
            {
                yield return new ViewData(Properties.Resources.airbrush_256, "item 1");
                yield return new ViewData(Properties.Resources.colors_256, "item 2");
                yield return new ViewData(Properties.Resources.distribute_left_edge_256, "item 3");
                yield return new ViewData(Properties.Resources.dossier_ardoise_images, "item 4");
            }
        }
    }
    
    public class ViewData
    {
        public ViewData(Bitmap icon, string name)
        {
            this._icon = icon;
            this._name = name;
        }
    
        private readonly Bitmap _icon;
        public Bitmap Icon
        {
            get
            {
                return this._icon;
            }
        }
    
        private readonly string _name;
        public string Name
        {
            get
            {
                return this._name;
            }
        }
    }
    

    In this solution I add existing PNG images to Properties.Resources class. Then icons has type Bitmap that is incompatible with Source property type, so we should convert it to BitmapSource with next converter:

    public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Bitmap)
            {
                var stream = new MemoryStream();
                ((Bitmap)value).Save(stream, ImageFormat.Png);
    
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.StreamSource = stream;
                bitmap.EndInit();
    
                return bitmap;
            }
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    On other hand you can use pack uri’s for storing icons instead of resources. Then your ViewData class will expose property of type Uri (instead of Bitmap). Then no converters are needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple question. I want to test whether a particular port
I am learning LINQ and have a very simple question that I think will
I have a very simple question. I want to write the below line of
I have a very simple question about menu control in Android. I'm writing a
I have a very simple question about rails console, if I want to get
I have a very simple question: After data are inserted, they will (anytime) be
I have a very simple question: I want to be able to save an
I have a very simple question, but I'd like to know : When and
I have a very simple question, so I hope someone will help. I have
I have a very simple question regarding BSTs. I have seen multiple definitions of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.