I´m trying to change the focused/selected item of a ListBox. My application is based on this article. At the moment I´m trying to set the ListBoxItem style via data templates:
<DataTemplate x:Key="ItemTemplate"> <TextBlock Text="{Binding}" Foreground="Black" FontFamily="Segoe UI" FontSize="22" HorizontalAlignment="Left" Padding="15,10,0,0" /> </DataTemplate> <DataTemplate x:Key="SelectedTemplate"> <TextBlock Text="{Binding}" Foreground="Red" FontFamily="Segoe UI" FontSize="30" HorizontalAlignment="Left" Padding="15,10,0,0" /> </DataTemplate> My idea was to switch between those templates using a trigger:
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle"> <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" /> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" /> </Trigger> </Style.Triggers> </Style> The ListBox looks like this:
<ListBox x:Name="valuesItemsCtrl" BorderThickness="0" ItemContainerStyle="{StaticResource ContainerStyle}" Background="Transparent" Tag="{Binding }"> <ListBox.AlternationCount> <Binding> <Binding.Path>Values.Count</Binding.Path> </Binding> </ListBox.AlternationCount> <ListBox.ItemsSource> <Binding> <Binding.Path>Values</Binding.Path> </Binding> </ListBox.ItemsSource> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel /> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox> At the end I add the template to another ListBox:
<ListBox x:Name="tumblersCtrl" BorderThickness="0" Background="Transparent" ItemsSource="{Binding Tumblers, ElementName=thisCtrl}" ItemTemplate="{StaticResource TumblerTemplate}"> </ListBox> Thanks for any help or hint!