0

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> 
5
  • Based on this page's example in the docs, it looks like you could name the stroke itself, and target that with the setter. msdn.microsoft.com/en-us/library/… Commented Mar 27, 2018 at 19:26
  • 1
    @Tim You can't use TargetName in a Style Setter. Commented Mar 27, 2018 at 19:29
  • @Clemens, well then that's the answer to the question, as he is definitely using one already. Commented Mar 27, 2018 at 19:30
  • @Tim XAML in the question won't compile... Commented Mar 27, 2018 at 19:32
  • As a note, the VirtualizingPanel properties have no effect, since you're using a Canvas as ItemsPanel. Also, DisplayMemberPath won't work. Commented Mar 27, 2018 at 19:34

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.