2016-11-21 43 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

相關問題