Change background color of selected usercontrol in listbox

Robby Smet

I have a listbox with some items. When an item is selected, I want to change the background color of the UserControlButton.

How can I do this?

<Window.Resources>
    <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    </Style>
</Window.Resources>

<Border x:Name="UserScrollContainer">
       <ListBox x:Name="UserContainer" ItemsSource="{Binding allUserViewModel.Users}" 
                             Background="Transparent"   
                             HorizontalContentAlignment="Stretch"
                             ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
                             ScrollViewer.VerticalScrollBarVisibility="Visible"
                             BorderThickness="0" Margin="0" Padding="0"    
                             ItemContainerStyle="{DynamicResource ListBoxItemStyle}">

            <ListBox.ItemTemplate>
                 <DataTemplate>
                      <local:UserControlButton x:Name="UserControlButton" />   
                                // Change background color depending if it is selected
                  </DataTemplate>
            </ListBox.ItemTemplate>
      </ListBox>
</Border>     

EDIT

I know I can add something like this:

        <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>

        <Setter Property="Background" Value="Lightblue"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="Yellow"/>
            </Trigger>
        </Style.Triggers>
    </Style>

but then I get this result:

enter image description here

I need to change the background of the usercontrol, not of the listboxitem.

stukselbax

You have several approaches to solve your problem. One of them I will describe here.

You can define <Style /> on your <UserContorl />, in order to reflect the ListBoxItem.IsSelected property:

<DataTemplate>
    <local:UserControlButton x:Name="UserControlButton">
        <local:UserControlButton.Style>
            <Style TargetType="{x:Type local:UserControlButton}">
                <Setter Property="Background" Value="Lightblue"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}" Value="true">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </local:UserControlButton.Style>
    </local:UserControlButton>
</DataTemplate>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Change background color of selected item in listbox

From Dev

Change background color of selected UITableViewCell

From Dev

Change background color of "selected" with CSS

From Dev

Change background color when selected

From Dev

how to change the background of the selected item in a listbox?

From Dev

how to change the background of the selected item in a listbox?

From Dev

Change ListBox Item Background color programmatically

From Dev

Change background color of the selected item in Android Spinner

From Dev

JTabbedPanel change selected tab background color

From Dev

how to change menu background color when selected?

From Dev

Change the selected tab background and text color of TabLayout

From Dev

UITableView: Change selected cell background color

From Dev

Swift: Background color change in selected segment of UISegmentControl

From Dev

JQuery to change background color of selected list item

From Dev

how to change menu background color when selected?

From Dev

Change text input background color on selected exit

From Dev

Thunderbird: change "selected" tags background color

From Dev

Change background on one List Item in a listbox (Not the selected item)

From Dev

How to change Listbox item color from selected Item(object)?

From Dev

How to change the background color of a ListBox Item when hovering?

From Dev

Change Background Color of SelectedItem in ListBox in .NET 4.5 WPF

From Dev

change ListBox's particular Item background color at bind time

From Dev

HorizontalContentAlign="Stretch" and change highlight background color in listbox Windows Phone 8

From Dev

How to change the background color of a ListBox Item when hovering?

From Dev

Change background color and font color of selected item in listview

From Dev

Title color of UIButton won't change on highlighted/selected but background color will

From Dev

Android ListView. How to change background color of manually selected item

From Dev

Android - how to change the background color of the selected item in a dropdown

From Dev

Change WPF treeViewItem Background color when selected but lost focus

Related Related

  1. 1

    Change background color of selected item in listbox

  2. 2

    Change background color of selected UITableViewCell

  3. 3

    Change background color of "selected" with CSS

  4. 4

    Change background color when selected

  5. 5

    how to change the background of the selected item in a listbox?

  6. 6

    how to change the background of the selected item in a listbox?

  7. 7

    Change ListBox Item Background color programmatically

  8. 8

    Change background color of the selected item in Android Spinner

  9. 9

    JTabbedPanel change selected tab background color

  10. 10

    how to change menu background color when selected?

  11. 11

    Change the selected tab background and text color of TabLayout

  12. 12

    UITableView: Change selected cell background color

  13. 13

    Swift: Background color change in selected segment of UISegmentControl

  14. 14

    JQuery to change background color of selected list item

  15. 15

    how to change menu background color when selected?

  16. 16

    Change text input background color on selected exit

  17. 17

    Thunderbird: change "selected" tags background color

  18. 18

    Change background on one List Item in a listbox (Not the selected item)

  19. 19

    How to change Listbox item color from selected Item(object)?

  20. 20

    How to change the background color of a ListBox Item when hovering?

  21. 21

    Change Background Color of SelectedItem in ListBox in .NET 4.5 WPF

  22. 22

    change ListBox's particular Item background color at bind time

  23. 23

    HorizontalContentAlign="Stretch" and change highlight background color in listbox Windows Phone 8

  24. 24

    How to change the background color of a ListBox Item when hovering?

  25. 25

    Change background color and font color of selected item in listview

  26. 26

    Title color of UIButton won't change on highlighted/selected but background color will

  27. 27

    Android ListView. How to change background color of manually selected item

  28. 28

    Android - how to change the background color of the selected item in a dropdown

  29. 29

    Change WPF treeViewItem Background color when selected but lost focus

HotTag

Archive