Styling for ListBox, ListView and GridView Items

2019-08-18 06:50发布

问题:

The default colors for these controls seems to be similar to the Windows theme colors. How do you change the hover, selected, selected hover and pressed colors (code or XAML)? The following isn't working for the ListView:

<ListView>
    <ListViewItemPresenter
        PointerOverBackground="#99CEEA"
        SelectedPressedBackground="#72BFE9"
        SelectedBackground="#72BFE9"
        SelectedPointerOverBackground="#99CEEA"
        />

回答1:

In your VS/Blend Designer, right click on your ListView and select

Edit Additional Templates > Edit Generated Item Container (ItemContainerStyle) > Edit a Copy...

In the popup window above, if you want this style to be applied to all your ListViewItem, select Apply to all otherwise just give it a name.

I'd recommend to create a new Resource dictionary for storing all ListView related styling. To do so, just hit the New... button and give the resource dictionary a name (e.g. ListViewStyles.xaml).

Finally, hit the OK button and you now have a fully generated style.

In the style's ControlTemplate, you can locate the ListViewItemPresenter control and update its colors accordingly.



回答2:

The ListViewItemPresenter was in the wrong place in the XAML. Change this:

<ListView>
    <ListViewItemPresenter
        PointerOverBackground="#99CEEA"
        SelectedPressedBackground="#72BFE9"
        SelectedBackground="#72BFE9"
        SelectedPointerOverBackground="#99CEEA"
        />
</ListView>

to this:

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <ListViewItemPresenter
                            PointerOverBackground="#99CEEA"
                            SelectedPressedBackground="#72BFE9"
                            SelectedBackground="#72BFE9"
                            SelectedPointerOverBackground="#99CEEA" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
<ListView>