2013-10-17 36 views
0

我正在使用WPF開發Windows應用商店應用程序。在主頁面中,我有一個涵蓋所有屏幕的背景圖片。我正在使用幾個ListBox es,並使用自定義ItemTemplate和透明背景。到目前爲止,它工作正常。如何更改列表框中所選項目的默認背景

這裏的事情是,當我從ListBox中選擇任何項目時,它會以紫色(我猜測爲默認值)突出顯示,並且ListBox的背景會變爲白色。

<ListBox x:Name="listBox1" Background="Transparent" BorderBrush="Transparent" 
     ItemTemplate=" {StaticResource listBox1DataTemplate}" 
     SelectionChanged="listBox1_SelectionChanged" 
     ItemContainerStyle="{StaticResource StylelistBox1}"/> 

我試圖改變它使用樣式和應用它時,該項目被選中。但是,它不起作用。

是否有人知道如何更改所選項目的默認顏色?

商祺!

+0

可能的複製[所選列表框項目更改背景顏色(http://stackoverflow.com/questions/2138200/change-background-color-for-selected-listbox-item/2138237 #2138237) –

+0

嗨。謝謝回覆。我已經看到你發佈的鏈接。但是,這裏給出的解決方案不適用於Windows 8操作系統。對於如何在Windwos 8中實現它有幾點意見。我會測試它們。 – MikePR

回答

0

也許您應該根據您的要求更改ListBoxItem的默認樣式模板。

<Style TargetType="ListBoxItem"> 
<Setter Property="Padding" Value="3" /> 
    <Setter Property="HorizontalContentAlignment" Value="Left" /> 
    <Setter Property="VerticalContentAlignment" Value="Top" /> 
    <Setter Property="Background" Value="Transparent" /> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="TabNavigation" Value="Local" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <Grid Background="{TemplateBinding Background}"> 
        <vsm:VisualStateManager.VisualStateGroups> 
         <vsm:VisualStateGroup x:Name="CommonStates"> 
          <vsm:VisualState x:Name="Normal" /> 
          <vsm:VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <DoubleAnimation Storyboard.TargetName="fillColor" Storyboard.TargetProperty="Opacity" Duration="0" To=".35"/> 
           </Storyboard> 
          </vsm:VisualState> 
          <vsm:VisualState x:Name="Disabled"> 
           <Storyboard> 
            <DoubleAnimation Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Opacity" Duration="0" To=".55" /> 
           </Storyboard> 
          </vsm:VisualState> 
         </vsm:VisualStateGroup> 
         <vsm:VisualStateGroup x:Name="SelectionStates"> 
          <vsm:VisualState x:Name="Unselected" /> 
          <vsm:VisualState x:Name="Selected"> 
           <Storyboard> 
            <DoubleAnimation Storyboard.TargetName="fillColor2" Storyboard.TargetProperty="Opacity" Duration="0" To=".75"/> 
           </Storyboard> 
          </vsm:VisualState> 
         </vsm:VisualStateGroup> 
         <vsm:VisualStateGroup x:Name="FocusStates"> 
          <vsm:VisualState x:Name="Focused"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Visibility" Duration="0"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Visible</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </vsm:VisualState> 
          <vsm:VisualState x:Name="Unfocused"/> 
         </vsm:VisualStateGroup> 
        </vsm:VisualStateManager.VisualStateGroups> 
        <Rectangle x:Name="fillColor" Opacity="0" Fill="#FFBADDE9" IsHitTestVisible="False" RadiusX="1" RadiusY="1"/> 
        <Rectangle x:Name="fillColor2" Opacity="0" Fill="#FFBADDE9" IsHitTestVisible="False" RadiusX="1" RadiusY="1"/> 
        <ContentPresenter 
          x:Name="contentPresenter" 
          Content="{TemplateBinding Content}" 
          ContentTemplate="{TemplateBinding ContentTemplate}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          Margin="{TemplateBinding Padding}"/> 
        <Rectangle x:Name="FocusVisualElement" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed" RadiusX="1" RadiusY="1" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

MSDN REFERENCE HERE

+0

嗨,奧斯卡。我在你發佈的代碼中做了類似的事情。現在它工作正常。我有一個自定義顏色的選定項目。 – MikePR

相關問題