UWP更改所选ListViewItem的样式

时间:2017-07-11 09:29:56

标签: xaml uwp

我有一个ListView。它不是动态的,我在这样的XAML中给它ListViewItemListView中的<ListView Name="ABC"> <ListViewItem> <StackPanel> <BitmapIcon/> <TextBlock/> </StackPanel> </ListViewItem> <ListViewItem> <StackPanel> <BitmapIcon/> <TextBlock/> </StackPanel> </ListViewItem> <ListViewItem> <StackPanel> <BitmapIcon/> <TextBlock/> </StackPanel> </ListViewItem> </ListView> 个很少。 XAML是这样的:

ListViewItem

选择Foreground后,我想将BitmapIconListViewItem的{​​{1}}属性更改为其他值。我如何实现这一目标。我愿意将ListView更改为一些较轻的控件,例如ItemsControl,只要我能够更改BitmapIcon的前景属性。

1 个答案:

答案 0 :(得分:2)

这应该在ListViewItem Style 中完成。您只需将SelectedForeground更改为您喜欢的任何颜色即可。例如 -

<Style x:Key="ListViewItemStyle1" TargetType="ListViewItem">
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter Property="Background" Value="{ThemeResource ListViewItemBackground}" />
    <Setter Property="Foreground" Value="{ThemeResource ListViewItemForeground}" />
    <Setter Property="TabNavigation" Value="Local" />
    <Setter Property="IsHoldingEnabled" Value="True" />
    <Setter Property="Padding" Value="12,0,12,0" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}" />
    <Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}" />
    <Setter Property="AllowDrop" Value="False" />
    <Setter Property="UseSystemFocusVisuals" Value="True" />
    <Setter Property="FocusVisualMargin" Value="0" />
    <Setter Property="FocusVisualPrimaryBrush" Value="{ThemeResource ListViewItemFocusVisualPrimaryBrush}" />
    <Setter Property="FocusVisualPrimaryThickness" Value="2" />
    <Setter Property="FocusVisualSecondaryBrush" Value="{ThemeResource ListViewItemFocusVisualSecondaryBrush}" />
    <Setter Property="FocusVisualSecondaryThickness" Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <ListViewItemPresenter CheckBrush="{ThemeResource ListViewItemCheckBrush}"
                                       ContentMargin="{TemplateBinding Padding}"
                                       CheckMode="{ThemeResource ListViewItemCheckMode}"
                                       ContentTransitions="{TemplateBinding ContentTransitions}"
                                       CheckBoxBrush="{ThemeResource ListViewItemCheckBoxBrush}"
                                       DragForeground="{ThemeResource ListViewItemDragForeground}"
                                       DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
                                       DragBackground="{ThemeResource ListViewItemDragBackground}"
                                       DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
                                       FocusVisualPrimaryBrush="{TemplateBinding FocusVisualPrimaryBrush}"
                                       FocusVisualSecondaryThickness="{TemplateBinding FocusVisualSecondaryThickness}"
                                       FocusBorderBrush="{ThemeResource ListViewItemFocusBorderBrush}"
                                       FocusVisualMargin="{TemplateBinding FocusVisualMargin}"
                                       FocusVisualPrimaryThickness="{TemplateBinding FocusVisualPrimaryThickness}"
                                       FocusSecondaryBorderBrush="{ThemeResource ListViewItemFocusSecondaryBorderBrush}"
                                       FocusVisualSecondaryBrush="{TemplateBinding FocusVisualSecondaryBrush}"
                                       HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                       Control.IsTemplateFocusTarget="True"
                                       PointerOverForeground="{ThemeResource ListViewItemForegroundPointerOver}"
                                       PressedBackground="{ThemeResource ListViewItemBackgroundPressed}"
                                       PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackground}"
                                       PointerOverBackground="{ThemeResource ListViewItemBackgroundPointerOver}"
                                       ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
                                       SelectedPressedBackground="{ThemeResource ListViewItemBackgroundSelectedPressed}"
                                       SelectionCheckMarkVisualEnabled="{ThemeResource ListViewItemSelectionCheckMarkVisualEnabled}"
                                       SelectedForeground="LightBlue"
                                       SelectedPointerOverBackground="{ThemeResource ListViewItemBackgroundSelectedPointerOver}"
                                       SelectedBackground="{ThemeResource ListViewItemBackgroundSelected}"
                                       VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

只要您未在Foreground上指定BitmapIcon属性,它就会在{{1}时自动获取更新的颜色(来自上述样式中的SelectedForeground)被选中。