1
votes

Consider a simple listbox:

        <ListBox ItemsSource="{Binding}">
            <ListBox.Resources>
                <SolidColorBrush x:Key="MyNormalForegroundBrush" Color="White"/>
                <SolidColorBrush x:Key="MyNormalBackgroundBrush" Color="Black"/>
                <SolidColorBrush x:Key="MySelectedForegroundBrush" Color="Purple"/>
                <SolidColorBrush x:Key="MySelectedBackgroundBrush" Color="Yellow"/>
            </ListBox.Resources>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="{StaticResource MyNormalForegroundBrush}"  BorderThickness="5" Background={StaticResource MyNormalBackgroundBrush}>
                        <TextBlock Text="{Binding}" Foreground="{StaticResource MyNormalForegroundBrush}" />
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

How can I get the border brush, background, and textblock text foreground to change color for the currently selected item?

1

1 Answers

0
votes

This is one of the ways provide for you, when the selected item changed, get the current selected item, and then find the control in DataTemplate by element name, after you got the controls, you can change the control style