【问题标题】:How to change default selected focused background color in ListView in UWP?如何在 UWP 的 ListView 中更改默认选定的焦点背景颜色?
【发布时间】:2016-07-01 11:30:49
【问题描述】:

我正在开发 Windows 通用应用程序。我想更改列表视图选择的背景颜色。系统默认选择的背景颜色是蓝色,我需要灰色。我使用数据模板将数据绑定到列表视图。

代码

数据模板

<Page.Resources>
<DataTemplate x:Name="datatemplate1" x:DataType="data:storedata">
            <Grid>
                <Grid.RowDefinitions>

                    <RowDefinition Height="*"/>

                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <
                </Grid.ColumnDefinitions>


                <TextBlock Grid.Column="0"
                           Text="{x:Bind name}" 
                           x:Phase="1"  
                           x:Name="product_name_layout"
                           Style="{ThemeResource BaseTextBlockStyle}"
                           TextWrapping="NoWrap" 
                            Foreground="#FF080808"                                                 
                           Margin="2,3,0,0"
                           FontSize="12"
                           />
                <TextBlock x:Name="status_title_layout"    FontSize="12" Foreground="Gray"   Style="{ThemeResource BaseTextBlockStyle}"    Visibility="Visible"  SelectionHighlightColor="#FFB1B4BE" Margin="2,5,0,0" Text="SKU:" Grid.Column="1" Grid.Row="1"></TextBlock>
                <TextBlock  Grid.Column="1"
                            Grid.Row="0"
                            Text="{x:Bind status}" 
                            x:Phase="2"
                            Foreground="Gray"
                             FontSize="12"
                             Style="{ThemeResource BaseTextBlockStyle}"
                            Margin="32,5,0,0"
                            x:Name="sku_layout"/>

            </Grid>
        </DataTemplate>
    </Page.Resources>

列表视图:

<ListView x:Name="MasterListView"                 
                  UseLayoutRounding="False"
                  ScrollViewer.VerticalScrollMode="Enabled"                 
                  BorderBrush="#FFA70EAA"
                  SelectionChanged="OnSelectionChanged"
                  IsItemClickEnabled="True"
                  ShowsScrollingPlaceholders="False"
                  ItemTemplate="{StaticResource datatemplate1 }"
                  ItemClick="OnItemClick"
                  Grid.Column="0"
                  Grid.Row="1"
                    >

            <ListView.ItemContainerStyle>


                <Style TargetType="ListViewItem">

                    <Setter Property="HorizontalContentAlignment"  Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>

        </ListView>

我还放了一些屏幕截图,以便您轻松理解。

图片

【问题讨论】:

    标签: windows xaml win-universal-app uwp


    【解决方案1】:

    将此模板添加到您的 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>
      </Setter.Value>
    </Setter>
    

    这里你可以删除或修改你想要的,看起来你想把SelectedBackground颜色改为DarkGrey。

    【讨论】:

    • 我刚刚编辑了这样的代码 SelectedBackground="DarkGrey" 但它不起作用。
    • 非常感谢您提供最佳解决方案。请您给我建议一件事。我的应用程序中有 20 个 Listview,我想将这个东西应用到所有的 listview 中,那么我该如何概括这种风格,以便每个 Listview 都受这种风格的影响。
    • 恐怕你不能重写原来的ListView模板,你必须把这个样式放到每个ListView上。最好的办法是将此样式放入 App.xaml 文件中,然后您可以在任何地方使用它而无需任何引用。
    猜你喜欢
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多