2016-11-21 42 views
1

我想更改ListViewItem的选定背景颜色。现在我注意到,默认值不会被覆盖。在UWP应用程序中设置所有ListView项目

<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="{StaticResource YellowBrush}" 
        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> 

this post它被提及的是,一个具有覆盖ItemContainerStyle属性。但是这只能在ListView本身上完成。

有没有选择覆盖应用程序中所有ListViewItem的样式? PS:人们可以重写像刷子一样的刷子。

<SolidColorBrush x:Key="SystemControlHighlightListAccentLowBrush" Color="Yellow" /> 

但我只想要风格ListViewItem而不影响其他风格。

编辑:

因为我也有

Visual Studio中需要Windows的更新版本,以显示此内容。请更新至Windows 10周年纪念版(10.0.14393.0)或更高版本。

我的目标版本设置为10240。这是我在App.xaml中发现由托马斯Schneiter陈述创建副本后:

<Application.Resources> 
    <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 
         CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" 
         ContentMargin="{TemplateBinding Padding}" 
         CheckMode="Inline" 
         ContentTransitions="{TemplateBinding ContentTransitions}" 
         CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" 
         DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}" 
         DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}" 
         DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}" 
         DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}" 
         FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}" 
         FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}" 
         HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
         PointerOverForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}" 
         PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}" 
         PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" 
         PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}" 
         ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}" 
         SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}" 
         SelectionCheckMarkVisualEnabled="True" 
         SelectedForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}" 
         SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}" 
         SelectedBackground="{ThemeResource SystemControlHighlightListAccentLowBrush}" 
         VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Application.Resources> 

现在我试图重写SelectedBackground,但这些更改不适用。

+0

将“ItemContainerStyle”的副本添加到您的App.xaml资源中,无需密钥即可将其设置为您的应用程序的默认值。 –

+0

你能从“ItemContainerStyle”发布一个拷贝的链接吗?我找不到一个,除了['ListViewItem'之一](https://msdn.microsoft.com/en-us/library/windows/apps/mt299136.aspx)。 – testing

+0

似乎你使用的是xamarin.form,并使用xamarin的'ListView'控件?你有解决这个问题吗? –

回答

2

您可以右键单击在设计视图中的任何ListView和选择Edit Additional Templates - >Edit Generated Item Container (Item Container Style) - >Edit a Copy
然后选择Apply to allApplication自动将全局副本插入到app.xaml中。

+0

现在我按照您的说明创建了副本(请参阅我编辑的问题),但该样式未得到应用。我相信这是因为没有创建“ItemContainerStyle”元素? – testing

+0

如果我将SelectedBackground改为另一种颜色,它将应用于应用程序中的所有ListView。 'ItemContainerStyle'属性应该从你的ListView中移除以使用全局定义的样式。 –

+0

现在我尝试了[来自这里](http://stackoverflow.com/a/33346834/426227)的示例,并且背景确实可以应用,但不适用于我的真实世界应用程序。我也尝试应用我的初始风格,在这里它也可以工作。也许是一个框架问题(Xamarin.Forms)。一旦我知道更多,我给你一些信息。 – testing

相关问题