Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a border to an item in a ItemsControl?

I am trying to set a border to each item from a items control. Following is my XAML code. But this doesn't work.

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="Control.BorderThickness" Value="5" />
        <Setter Property="Control.BorderBrush" Value="Black" />
    </Style>
</ItemsControl.ItemContainerStyle>
like image 498
Lucifer Avatar asked Jan 29 '12 18:01

Lucifer


2 Answers

The container in an ItemsControl is a ContentPresenter which is not a control, this style will not do anything. You could create an ItemsTemplate containing a Border.

Alternatively you can change the ContentTemplate in the ItemContainerStyle:

<ItemsControl.ItemContainerStyle>
    <Style TargetType="ContentPresenter">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="5">
                        <ContentPresenter Content="{Binding}"/>
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ItemsControl.ItemContainerStyle>

(Note: This is a real alternative in the sense that it does the exact same thing, so i would use the ItemTemplate as it is a lot less verbose, saves you three tags (Style, Setter, Setter.Value))

like image 106
H.B. Avatar answered Nov 05 '22 22:11

H.B.


See the remarks on BorderThickness and [BorderBrush][1]:

This property only affects a control whose template uses the BorderThickness property as a parameter.On other controls, this property has no impact.

This property only affects a control whose template uses the BorderBrush property as a parameter.On other controls, this property has no impact.

So you actually need such a control, e.g. Border in which you wrap whatever you need to display.

like image 32
Joey Avatar answered Nov 06 '22 00:11

Joey