none
Use ListBox with C# code

    Question

  • Hi!,
    I would like to use ListBox using C# and not XAML. There are several examples on how to do it with XAML, but no samples on how to display Thumbnails of images in a Listbox using C# to declare and setup the listbox. Any help would be greatly apprceciated.

    Thanks,
    CACUser
    CACUser
    Wednesday, July 09, 2008 6:40 PM

Answers

  • Change the following code

    ListBoxItem item = new ListBoxItem();
    item.Content = thumbnail;
    leftListBox.Items.Add(item);

    To

    ListBoxItem item = new ListBoxItem();
    item.Content =
    new Image(){ Source = thumbnail };
    leftListBox.Items.Add(item);

    Hope this helps
    • Marked as answer by Marco Zhou Wednesday, July 16, 2008 10:15 AM
    Monday, July 14, 2008 3:49 AM

All replies

  • Well if you know how to declare the list box it should not be too hard to finish it off.

    Just like in XAML the listbox in code has the same properties you can edit such as ItemsSource, so if you have an observable collection you can link that in there to get the list of items to display.

    Now to make thumbnails you need to make a template just like you would do in XAML.

    So you can either make the datatemplate in XAML and reference it in code or define the template in code(slightly more annoying)

    To do it in code (example of declaring a textBlock data template):
    DataTemplate itemTemplate = null
     
    Binding myBinding = new Binding(); 
    myBinding.Converter = new PropertyValueConverter(); 
    myBinding.ConverterParameter = property; 
     
    FrameworkElementFactory textBlockFactory = new FrameworkElementFactory(typeof(TextBlock)); 
    textBlockFactory.SetBinding(TextBlock.TextProperty, myBinding); 
    textBlockFactory.SetBinding(TextBlock.ToolTipProperty, myBinding); 
    textBlockFactory.SetValue(TextBlock.TextTrimmingProperty, TextTrimming.CharacterEllipsis); 
    textBlockFactory.SetValue(TextBlock.MarginProperty, new Thickness(4, 1, 4, 1)); 
    textBlockFactory.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center); 
                                            
    itemTemplate= new DataTemplate(); 
    itemTemplate.VisualTree = textBlockFactory; 
     
    //then 
     
    lb.ItemTemplate = itemTemplate; 

    assuming lb is your listbox you defined

    So you need to use FrameworkElementFactory to define the type of content item you want.  You can also declare multiples and nest them as you would in XAML
    Wednesday, July 09, 2008 7:26 PM
  • I am not sure if you are asking for the easy way like:

    ListBox lb = new ListBox();
     lb.Items.Add(new Image());   where you can insert your thumbnail instead of the new image

    would answer you? Or were you asking for the more detailed version as above?

    Noorbakhsh
    • Edited by noorbakhsh Wednesday, July 09, 2008 9:09 PM Typo
    Wednesday, July 09, 2008 9:08 PM
  • I do not have an observable collection. I have a bunch of images which are in an array. I loop thru the array and process the images. At the end of the processing pipeline I would like to add the image as a thumbnail to a listbox that is part of the page. I am able to add text - example the name of the image, but not a thumbnail of the image.

    Currently this is what I have _ It shows the filename of the image.I would like to change the settings of the listbox to show the thumbnail image:

    System.Windows.Controls.ListBox leftListBox = new System.Windows.Controls.ListBox();

    After processing the image on a different thread than the UI thread: from the child thread:

    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.DecodePixelWidth = 100;
    bi.CacheOption =
    BitmapCacheOption.OnLoad;
    bi.UriSource = b.FilenameUri;
    bi.EndInit();
    bi.Freeze();

    try
    {
    this
    .Dispatcher.BeginInvoke(
    System.Windows.Threading.
    DispatcherPriority.Normal,(UpdateTheUI2)delegate(BitmapSource thumbnail)
    {
    ListBoxItem item = new ListBoxItem();
    item.Content = thumbnail;
    leftListBox.Items.Add(item);
    }, (
    BitmapSource)bi);
    }
    catch (Exception ex)
    {
    System.Diagnostics.
    Debug.WriteLine("Display:" + ex);
    }


    CACUser
    Wednesday, July 09, 2008 9:21 PM
  • Change the following code

    ListBoxItem item = new ListBoxItem();
    item.Content = thumbnail;
    leftListBox.Items.Add(item);

    To

    ListBoxItem item = new ListBoxItem();
    item.Content =
    new Image(){ Source = thumbnail };
    leftListBox.Items.Add(item);

    Hope this helps
    • Marked as answer by Marco Zhou Wednesday, July 16, 2008 10:15 AM
    Monday, July 14, 2008 3:49 AM