Search code examples
wpflistboxitemcontentpresenter

ListItem Template - content not showing


I'm trying to create a new style for a ListItem to get rid of the default blue rectangles showing when hovering the items and selecting them. here's what I've got so far:

<Style TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel Orientation="Horizontal">
                        <ContentPresenter/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Problem is - content is not showing at all. I tried to do it with a different controller (a button, for example). ContentPresenter doesn't seem to work with ListBoxItem though...

How do I achieve that?


Solution

  • You need to add TargetType="ListBoxItem" to ControlTemplate

     <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <StackPanel Orientation="Horizontal">
                                <ContentPresenter/>
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>