-3

In my application I have a List of strings, however I can't find out how to/don't know how to bind this list to a StackPanel.

I've tried using a ListBox but the scrolling nature of the ListBox is very inconvenient to the user of my application.

So does anyone know how I could bind a List of strings to a StackPanel?

I have tried messing around with several properties but haven't found anything.

Thanks for your help!

Newbie
  • 1,160
  • 2
  • 11
  • 24
  • 1
    What have you tried? I am quite sure that there are a ton of examples out there on the internet. Please do some research and show effort opposed to expecting others to give you the answer and or do your work Thanks – MethodMan Aug 12 '13 at 13:10

1 Answers1

5

To bind an enumerable to a control and have them displayed, you can use any of the ItemsControl and bind your object to the ItemsSource property. The ItemsControls expose a property called ItemsPanel in which you can further modify to alter the container for the items. StackPanel is the default in most of them.

<ItemsControl ItemsSource="{Binding NewbieList}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <!-- The default for an ItemsControl is a StackPanel with a vertical orientation -->
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

Edit:

As for your comment, anything within the ItemsSource will "output" what's in the ItemTemplate property (the default is basicaly a TextBlock with the text bound to the DataContext). For each elements, the DataContext will be the item in the list. If you have a list of string, for example, you can do:

<!-- Rest is omitted for succinctness -->
<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding .}" FontSize="26" MouseDown="yourEventHandler"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
<ItemsControl>

Alternatively, if all you want is the font size to change, you can use the TextElement.FontSize Dependency Property on the ItemsControl itself or style the ItemsContainer:

<ItemsControl TextElement.FontSize="26">
    <!-- Rest omitted for succinctness -->
</ItemsControl>

Or:

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="TextElement.FontSize" Value="26"/>
    </Style>
</ItemsControl.ItemContainerStyle>

I suggest you read articles / tutorials on binding and items control in WPF for more information on how to do various tasks, there is alot to explain.

Simon Belanger
  • 14,752
  • 3
  • 41
  • 35
  • That almost works, but I need to be able to retrieve a click event for each individual item in the template, and I don't seem to be able to do that with the StackPanel – Newbie Aug 12 '13 at 15:58
  • @Newbie See my edit. You should definitely read some tutorials on WPF for more information on bindings, data templates and items control. – Simon Belanger Aug 12 '13 at 16:00
  • Yeah I tried just using a `TextBlock` but that brought up an UnhandledException, sorry that you went to all the trouble of answering my deleted comment, I figured that out by just adding `FontSize="32"` under the `Items.Control`. – Newbie Aug 12 '13 at 16:31