Styling for ListBox, ListView and GridView Items

こ雲淡風輕ζ 提交于 2019-12-25 12:45:17

问题


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>


来源:https://stackoverflow.com/questions/45800426/styling-for-listbox-listview-and-gridview-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!