Search code examples
wpflistboxitemtemplateuielement

Put UIElement to ListBox with custom ItemTemplate


I have an array of System.Windows.Controls.Image and I assign it to ListBox.ItemsSource.

What's more, I want to have a Border around each Image.

The xaml below demonstrates my idea.

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="2" Style="{StaticResource borderStyle}"
                    Child="{Binding}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

As you know, Child is not a dependency property; the code won't work.

So how should I put the Image (or the ListBox item) in the template?


Solution

  • A ContentControl should do the trick:

    <Border BorderThickness="2" Style="{StaticResource borderStyle}">
      <ContentControl Content="{Binding}"/>
    </Border>