Take the 2-minute tour ×

I am creating a timeline control that uses listbox to place my items on a canvas. There are two parts to it. The base timeline to represent an 8 hour work day and the individual timeline elements which represent the type of work with start and end times.

The timeline element can be resized using a thumb control docked to the left and right of each element. The resizing works fine if I place the timline element on a canvas directly, but as I need to maintain a list of elements I thought that a listbox would handle it for me. So I made the ItemsPanel a canvas and sure enough everything rendered nicely. The only problem was that I could no longer resize the timeline elements. Quickly I relized, that setting the canvas.Left on my element when dragging would do nothing because the element was no longer on the canvas but inside a listboxitem (which is on the canvas).

Is it possible to have a Listbox stop wrapping each item in its own listboxitem and wrap them in say a custom ContentControl instead?

share|improve this question
Is this WPF?... –  Ilya Kogan Jan 2 '11 at 6:11
@Ilya: It must be. He's talking about a Canvas, so that's definitely not WinForms. –  Cody Gray Jan 2 '11 at 6:14
Yes its in WPF. –  John Petrak Jan 2 '11 at 6:16

2 Answers 2

up vote 2 down vote accepted

Try this:

class MyListBox : ListBox
    protected override System.Windows.DependencyObject GetContainerForItemOverride()
        return new ContentControl();

Reference: How to change the default item container for ItemsControl?

share|improve this answer
Thats exactly what I needed. Thanks heaps. –  John Petrak Jan 5 '11 at 23:53

It is possible by adding a UIElement directly to ListBox.Items (or ItemsSource). However, I'd recommend instead using ItemContainerStyle to change Canvas.Left and Canvas.Top. This style allows you to control properties on the container (the ListBoxItem) generated for each item in your ItemsControl.

<ListBox ItemsSource="{Binding MyItems}">
        <Style TargetType="ListBoxItem">
            <Setter Property="Canvas.Left" Value="{Binding SomeLeftProperty}"/>
            <Setter Property="Canvas.Top" Value="{Binding SomeTopProperty}"/>
share|improve this answer
Thats exactly what I have now and displays fine, the problem comes when I need to drag the thumb control on an timeline element to resize it. It cant work as the timeline element (which is a ContentControl) is not directly on the canvas. its wrapped up in the ListboxItem. I would need to apply the thumb control to the listboxitem instead for it to work. –  John Petrak Jan 2 '11 at 9:51

Your Answer


