10

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>
H.B.
  • 166,899
  • 29
  • 327
  • 400
Lucifer
  • 2,317
  • 9
  • 43
  • 67

2 Answers2

27

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))

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • By "the exact same thing", H.B. means that `ItemsControl.ItemsTemplate` sets the `ContentTemplate` of each item container. This means that if you do both, the `ItemTemplate` will completely replace the `ItemContainerStyle`'s `ContentTemplate` value. – Artfunkel Sep 22 '17 at 08:24
2

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.

Community
  • 1
  • 1
Joey
  • 344,408
  • 85
  • 689
  • 683