How to change the background color of listview's item? RRS feed

  • Question

  • <ListView Name="MenuList" SelectionMode="None" IsItemClickEnabled="True" ItemClick="MenuList_ItemClick_1" HorizontalAlignment="Left" VerticalAlignment="Center" Background="AliceBlue">
                                <StackPanel Orientation="Horizontal" />
                                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                    <TextBlock Text="{Binding name}" VerticalAlignment="Center" FontSize="20" ></TextBlock>           


    in the click method "MenuList_ItemClick_1" ,I want to change the background color of current listView item I clicked,how to make this?thanks

    Wednesday, December 18, 2013 11:13 AM


  • Can you explain the scenario more clearly? Do you want to change the selection colour or something more active?

    You can change the selection and focus behavior by editing the ItemContainerStyle. Select the ListView in the designer and choose Edit Additional Templates.Edit Generated Item Container from the context menu.

    For other scenarios, the object for the clicked item is passed in the ItemClickEventArgs.ClickedItem argument. You can encode colour information in the object and bind the ItemTemplate's background colour to that property. You'll likely want to base this on some logical state rather than encoding colour directly, and can use a ValueConverter to change the state to a colour.

    Either way, you'll want to reference the colour from a resource dictionary so you can use an alternate colour for high contrast mode. If you're explicitly handling the ItemClick event also make sure you provide a way to get the same behavior via the keyboard.


    Wednesday, December 18, 2013 10:34 PM