2012-11-03 20 views
1

그래서,이 ListBox가 XAML로 작성이 : 나는 배경, 전경의 색상 및 목록 상자 항목의 텍스트를 변경할 수ListBox의 선택된 항목 색상을 변경할 수 있습니까?

<ListBox Margin="0" x:Name="ListBox_Main" Grid.Row="1" VerticalAlignment="Top" FontSize="24" SelectionChanged="ListBox_Main_SelectionChanged" Foreground="Black"> 
    <ListBoxItem Content="Item 1" Background="#19000000" /> 
    <ListBoxItem Content="Item 2" Background="#19000000" /> 
    <ListBoxItem Content="Item 3" Background="#19000000" /> 
    <ListBoxItem Content="Item 4" Background="#19000000"/> 
    <ListBoxItem Content="Item 5" Background="#19000000"/> 
</ListBox> 

하지만 색상을 변경하는 옵션이 없을 때 항목의 가 선택되면 기본적으로 파란색입니다. enter image description here

푸른 색을 바꿀 수 있습니까?

+0

필요한 모든 정보가 있습니다. http://stackoverflow.com/questions/5519845/change-the-selected-color-listbox – LightStriker

답변

0

귀하의 요구 사항에 따라 ListBoxItem의 기본 스타일 템플릿을 변경해야합니다. MSDN Reference

<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> 

당신은이 링크를 참조 Formatting the Background Color of a Selected WPF ListBox Item에 모습을 가질 수있다.