Jake
Jake

Reputation: 955

Correct Styling of Listboxes in WPF

I'm struggling at the moment to style my listbox using XAML as when I apply my style, all the content in my listbox disappears. I'm applying this style:

<Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="#2a82bb" BorderThickness="1" Background="White">
                    <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And calling it in my list item using:

Style="{StaticResource ListBoxStyle}"

Does something have to go into the content presenter tag or would I also need to style each list item separately since I'm overriding the default style for the listbox? Any examples would be great as the ones on the MSDN website aren't explained at all.

Upvotes: 0

Views: 767

Answers (2)

Heena
Heena

Reputation: 8634

try this

<Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                    <Border BorderBrush="#2a82bb" BorderThickness="1" Background="White"/>                       
                    <ContentPresenter/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Upvotes: 0

dkozl
dkozl

Reputation: 33364

ListBox is items control so when you create template instead of ContentPresenter use ItemsPresenter:

<ControlTemplate>
   <Border BorderBrush="#2a82bb" BorderThickness="1" Background="White">
      <ItemsPresenter/>
   </Border>
</ControlTemplate>

Upvotes: 1

Related Questions