2012-09-04 143 views
14

我試圖在WinRT頁面(XAML)上的「列表框」上更改背景顏色。當我使用「背景」屬性時,它會在控件沒有焦點時更改背景的方式。當它獲得焦點時,它變成白色,我無法弄清楚如何覆蓋它。列表框背景顏色(XAML/WinRT/Metro)

我的問題,如何強制列表框的背景始終爲灰色無論它是否被選中/是否有焦點?

XAML#1:

<ListBox x:Name="ListBoxMenu" Background="LightGray" Grid.Row="0" Grid.Column="0" Margin="0,0,0,0"> 
     <ListBoxItem>Menu Item 1</ListBoxItem> 
     <ListBoxItem>Menu Item 2</ListBoxItem> 
     <ListBoxItem>Menu Item 3</ListBoxItem> 
    </ListBox> 

XAML#2(與每個項目還設置):

<ListBox x:Name="ListBoxMenu" Background="LightGray" Grid.Row="0" Grid.Column="0" Height="124" VerticalAlignment="Top"> 
     <ListBoxItem Background="LightGray">Menu Item 1</ListBoxItem> 
     <ListBoxItem Background="LightGray">Menu Item 2</ListBoxItem> 
     <ListBoxItem Background="LightGray">Menu Item 3</ListBoxItem> 
    </ListBox> 

ListBox with Gray background when it doesn't have the focus

ListBox, resetting the background to white when it gets focus

作爲臨時的解決方案,我設置列表框只能是一個硬編碼的h八個,然後在該列上使用邊框用LightGray填充剩餘空間。我真的很想總是在ListBox上設置背景顏色,這是可能的嗎?

+0

您能否爲您提供的解決方案提供一些代碼片段?我也有同樣的問題,但無法修復。 – SachiraChin

+0

根據您的喜好,如果只有一個或兩個觸發背景變化的事件,您可以簡單地將ListBoxMenu.Background = Colors.Transparent添加到事件處理程序。 – Hong

回答

5

使用Visual Studio Blend 2012並編輯ListBox ItemTemplate或它的模板,它將在XAML中創建一個硬拷貝,您可以在其中編輯它的屬性。

+0

謝謝,讓我試試看。 –

+0

什麼是Visual Blender?你能提供一個鏈接嗎? –

+1

我認爲他的意思是Blend for Visual Studio。我能夠右鍵單擊列表框並使用編輯樣式創建一個可以編輯的硬拷貝。我跳過了Blend,因爲我能夠更改它提供的模板。 –

3

我遇到了同樣的問題,我用Visual Studio Blend的幫助。希望這可以幫助。

添加樣式到ListBoxMenu如下:

<ListBox x:Name="ListBoxMenu" Style="{StaticResource ListBoxStyle1} Background="LightGray" Grid.Row="0" Grid.Column="0" Height="124" VerticalAlignment="Top"> <ListBoxItem Background="LightGray">Menu Item 1</ListBoxItem> <ListBoxItem Background="LightGray">Menu Item 2</ListBoxItem> <ListBoxItem Background="LightGray">Menu Item 3</ListBoxItem> </ListBox>

然後指定樣式如下:

<Style x:Key="ListBoxStyle1" TargetType="ListBox"> 
     <Setter Property="Foreground" Value="{StaticResource ListBoxForegroundThemeBrush}"/> 
     <Setter Property="Background" Value="{StaticResource ListBoxBackgroundThemeBrush}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource ListBoxBorderThemeBrush}"/> 
     <Setter Property="BorderThickness" Value="{StaticResource ListBoxBorderThemeThickness}"/> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled"/> 
     <Setter Property="ScrollViewer.IsHorizontalRailEnabled" Value="True"/> 
     <Setter Property="ScrollViewer.VerticalScrollMode" Value="Enabled"/> 
     <Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="True"/> 
     <Setter Property="ScrollViewer.ZoomMode" Value="Disabled"/> 
     <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False"/> 
     <Setter Property="ScrollViewer.BringIntoViewOnFocusChange" Value="True"/> 
     <Setter Property="IsTabStop" Value="False"/> 
     <Setter Property="TabNavigation" Value="Once"/> 
     <Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/> 
     <Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/> 
     <Setter Property="ItemsPanel"> 
      <Setter.Value> 
       <ItemsPanelTemplate> 
        <VirtualizingStackPanel/> 
       </ItemsPanelTemplate> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBox"> 
        <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{StaticResource AppBarBackgroundThemeBrush}"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="LayoutRoot"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxDisabledForegroundThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Focused"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ScrollViewer"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="Black"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
           <VisualState x:Name="Unfocused"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <ScrollViewer x:Name="ScrollViewer"> 
         <ItemsPresenter/> 
        </ScrollViewer> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

你箱集裝箱到黑名單上述樣品將更換背景當焦點被設置到列表框時。

2

如果您需要在ListBoxListViewGridView,他們都在同一工作原理的定製的Items顏色一些更多的幫助,只是一定要更新TargetType性,我建議在看看維託DeMercurio的博客文章Styling a GridViewItem in WinRT

6

您可以在XAML資源文件中添加一些顏色筆刷覆蓋來覆蓋默認的ListBox控件模板顏色。

<SolidColorBrush x:Key="ListBoxBackgroundThemeBrush" Color="Transparent" /> 
<SolidColorBrush x:Key="ListBoxFocusBackgroundThemeBrush" Color="Transparent" /> 
+0

好的解決方案,只要你想要所有的ListBoxes相同的顏色。 –

+1

我認爲這不再適用於Windows 8。只在Win 7中。 – user3595338