user746461
user746461

Reputation:

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?

Upvotes: 1

Views: 661

Answers (1)

ColinE
ColinE

Reputation: 70162

A ContentControl should do the trick:

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

Upvotes: 2

Related Questions