JokerMartini
JokerMartini

Reputation: 6147

Change listbox item style when selected in WPF

I'm not entirely sure what I'm doing incorrect but it appears that my style trigger is not being recognized. I want to change the color of the Stroke when the listbox item is selected.

<ListBox ItemsSource="{Binding CityList}" DisplayMemberPath="Name" SelectionMode="Extended"
                 VirtualizingPanel.IsVirtualizing="true"
                 VirtualizingPanel.VirtualizationMode="Recycling"
                 Background="Brown">

            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Canvas.Left" Value="{Binding Longitude, Converter={StaticResource longValueConverter}, ConverterParameter={StaticResource mapWidth}}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Latitude, Converter={StaticResource latValueConverter}, ConverterParameter={StaticResource mapHeight}}"/>
                    <Setter Property="BorderThickness" Value="3" />

                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <Grid>
                                    <Ellipse x:Name="indicator" 
                                             Fill="#FF000000" 
                                             Height="10" 
                                             Width="10" 
                                             Stroke="Transparent"
                                             StrokeThickness="2"/>
                                </Grid>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="indicator" Property="Stroke" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>

            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas IsItemsHost="True"
                            Width="{StaticResource mapWidth}"
                            Height="{StaticResource mapHeight}"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

        </ListBox>

Upvotes: 0

Views: 1479

Answers (1)

Clemens
Clemens

Reputation: 128061

You can't use TargetName in a Style Setter.

Instead of setting the ContentTemplate property, you may set the Template property and add the Trigger to the ControlTemplate.Triggers collection:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="ListBoxItem">
            <Grid>
                <Ellipse x:Name="indicator" 
                         Fill="#FF000000" 
                         Height="10" 
                         Width="10" 
                         Stroke="Transparent"
                         StrokeThickness="2"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter TargetName="indicator" Property="Stroke" Value="Red"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

You may also add a ContentPresenter to the Grid in the ControlTemplate, which would display the elements of the ItemTemplate (if you later decide to declare one).

Upvotes: 3

Related Questions