【问题标题】:uwp win10 Listview SelectedItem Styleuwp win10 Listview SelectedItem 样式
【发布时间】:2016-02-29 17:07:33
【问题描述】:

有没有办法在选择 ListviewItem 时更改它的属性?

例如,我希望 ListviewItem 内的矩形在选中时为红色,默认为蓝色。

如何以优雅的方式实现这一点?

【问题讨论】:

    标签: c# windows win-universal-app


    【解决方案1】:

    您可以设置ListView.ItemContainerStyle,自定义ListViewItemsListView中使用的样式。

    此页面显示默认样式:https://msdn.microsoft.com/en-us/library/windows/apps/mt299136.aspx

    在您的示例中 - 您可以在类似于以下的代码中更改 Selected~Background 属性:

    <ListView ...>
        <ListView.ItemContainerStyle>
            <Style
                TargetType="ListViewItem">
                <Setter Property="Template">
                    <Setter.Value>
        <ControlTemplate TargetType="ListViewItem">
          <ListViewItemPresenter
              ContentTransitions="{TemplateBinding ContentTransitions}"
              SelectionCheckMarkVisualEnabled="True"
              CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
              CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
              DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
              DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
              FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}"
              FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}"
              PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
              PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
              PointerOverForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
              SelectedBackground="{ThemeResource SystemControlHighlightListAccentLowBrush}"
              SelectedForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
              SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}"
              PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
              SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}"
              DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
              DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
              ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
              ContentMargin="{TemplateBinding Padding}"
              CheckMode="Inline"/>
        </ControlTemplate>
    

    【讨论】:

    • 另外,如何使用此样式更改 SelectedItem 的 BorderBrush?
    【解决方案2】:

    对于以后发现此问题的任何人,我已通过 Nuget 中的一个库解决了此问题:https://github.com/JerryNixon/Template10.ListHelpers

    它的行为对每个状态使用不同的样式。

    <Style x:Key="ItemNormalStyle" TargetType="Grid">
        <Setter Property="RequestedTheme" Value="Dark" />
        <Setter Property="Background" Value="{ThemeResource ButtonPointerOverBackgroundThemeBrush}" />
    </Style>
    
    <Style x:Key="ItemSelectedStyle" TargetType="Grid">
        <Setter Property="RequestedTheme" Value="Light" />
        <Setter Property="Background" Value="{ThemeResource ButtonBackgroundThemeBrush}" />
    </Style>
    

    使用它也非常简单。这是一个附加属性。

    <ListView 
        helpers:ListViewHelper.SelectedItemStyle="{StaticResource MySelectorInfo}"
        ItemTemplate="{StaticResource ListViewItemTemplate}" >
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Padding" Value="0" />
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
    

    //祝你好运

    【讨论】:

      【解决方案3】:

      我已经在其他地方回答了这个问题,请检查一下! UWP gridview item selection style

      【讨论】:

      • 嗯,这肯定行得通,我只是更喜欢我的回答,不推荐你的回答。 :)
      • 感谢您的回答,菲利普。我已经按照您呈现的方式准备了 xaml。 Ricardo 的代码只是让我更进一步解决了这个问题。我投了两个答案,但我选择了他的。再次感谢你们。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 2018-05-25
      相关资源
      最近更新 更多