Search code examples
c#wpfdata-binding

How to binding UserControl in a StackPanel?


My question is simple, can I use binding in a StackPanel from elements in a LinkedList? In my application the order of the elements in the LinkedList is importante. May the elements can change its positions in the LinkedList on the fly. I want to know if its possible to bind my LinkedList so my app always displays the elements in the correct order without any additional C# code/Event triggers.

Its possible to do this in WPF?

How can I implement this?


Solution

  • Just set or bind the ItemsSource property of an ItemsControl to your linked list:

    ic.ItemsSource = yourList;
    

    ...and optionally define a custom ItemsPanelTemplate for the ItemsControl:

    <ItemsControl x:Name="ic">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>