-1

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?

  • Have you tried anything to achieve this? If so, where are you stuck, what are your issues with your solution? – Paul Kertscher Nov 03 '21 at 12:17
  • 3
  • 1
    `LinkedList` implements `ICollection` and `IEnumerable` hence I'd assume that it would be possible to bind to and that the order would be maintained. There already is an answer on how to bind to a `StackPanel`: https://stackoverflow.com/questions/18187784/how-can-you-bind-a-liststring-to-a-stackpanel – Paul Kertscher Nov 03 '21 at 12:22

1 Answers1

1

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>
mm8
  • 163,881
  • 10
  • 57
  • 88