2016-08-01 50 views
0

里面我有一个可点击的,GridView的insde一个HubSection:如何改变SelectedBackground在ListViewItemPresenter一个GridView

<HubSection > 
        <DataTemplate> 
         <GridView IsItemClickEnabled="True" ItemClick="Hub_OnClick"> 
          <RelativePanel > 
          <TextBlock Text="NiceText" /> 
          </RelativePanel> 
         </GridView> 
        </DataTemplate> 
       </HubSection> 

现在每次我点击了该集线器,蓝色边框周围会出现在GridView(SelectedBackground )。

在LiveVisualTree中,它显示了我的边框来自GridViewItem中的“ListViewItemPresenter”控件。因此我修改了原始控件的样式,并将其粘贴到Page.Resources标签中。

<!-- Default style for Windows.UI.Xaml.Controls.ListViewItem --> 
<Style TargetType="ListViewItem"> 
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" /> 
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" /> 
<Setter Property="Background" Value="Transparent"/> 
<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" /> 
<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="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}" 
      <!-- here --> 
      SelectedBackground="White" 
      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> 
</Style> 

但这不适用于我。 SelectedBackground-Border仍然是蓝色的。但为什么?我的错误在哪里?

回答

3

您应该覆盖GridViewItem样式并将新样式设置为GridView。在页面的资源声明你的新发型:

<Page.Resources> 
    <Style TargetType="GridViewItem" x:Key="CustomGridViewStyle"> 
     <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" /> 
     <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" /> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" /> 
     <Setter Property="TabNavigation" Value="Local"/> 
     <Setter Property="IsHoldingEnabled" Value="True"/> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
     <Setter Property="Margin" Value="0,0,4,4"/> 
     <Setter Property="MinWidth" Value="{ThemeResource GridViewItemMinWidth}"/> 
     <Setter Property="MinHeight" Value="{ThemeResource GridViewItemMinHeight}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="GridViewItem"> 
        <ListViewItemPresenter 
        ContentTransitions="{TemplateBinding ContentTransitions}" 
        SelectionCheckMarkVisualEnabled="True" 
        CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" 
        CheckBoxBrush="{ThemeResource SystemControlBackgroundChromeMediumBrush}" 
        DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}" 
        DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}" 
        FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}" 
        FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}" 
        PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" 
        PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}" 
        PointerOverForeground="{ThemeResource SystemControlForegroundBaseHighBrush}" 
        SelectedBackground="Transparent" 
        SelectedForeground="{ThemeResource SystemControlForegroundBaseHighBrush}" 
        SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}" 
        PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}" 
        SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}" 
        DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}" 
        DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}" 
        ReorderHintOffset="{ThemeResource GridViewItemReorderHintThemeOffset}" 
        HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
        VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
        ContentMargin="{TemplateBinding Padding}" 
        CheckMode="Overlay"/> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Page.Resources> 

和设置:

<HubSection > 
    <DataTemplate> 
     <GridView IsItemClickEnabled="True" 
       ItemClick="Hub_OnClick" 
       ItemContainerStyle="{StaticResource CustomGridViewStyle}"> 
      <RelativePanel > 
       <TextBlock Text="NiceText" /> 
      </RelativePanel> 
     </GridView> 
    </DataTemplate> 
</HubSection> 

附:您也可以覆盖Exanded style