2014-03-30 1 views
0

다시 간단한 질문입니다. ListBoxItems (그냥 일반 텍스트)로드 된 ListBox Windows 8 전화 응용 프로그램이 있습니다. 나는 XAML에서 흰색으로 텍스트의 전경색을 설정ListBox isSelected 메서드가 텍스트 스타일을 변경합니다.

<ListBox x:Name="L1" Foreground="white"> 

내가리스트 박스의 selectedIndex 속성, 빨간색으로 전경의 변화를 호출하면. 내가 xaml 또는 C#으로 설정했는지 여부는이 작업을 수행합니다. 나는 시도하고 selectedIndex의 호출 된 후 수동으로 색상을 변경 C#에서 코드를 추가하는 경우, 그것은 여전히 ​​...

tempListBoxItem = listBoxPicType.SelectedItem as ListBoxItem; 
tempListBoxItem.Foreground = //some color that isn't red 

이 주위에 얻을 수있는 가장 간단한 방법은 무엇을 작동하지 않는 이유는 무엇입니까? TIA

+0

가능한 복제본 [WP7 ListBox 선택한 항목이 색상을 변경하지 않습니다] (http://stackoverflow.com/questions/20003028/wp7-listbox-selected-item-is-not-changing-the-color) –

+0

제목에서 태그를 제거했습니다. 대부분의 경우 [제목에 제목을 넣으면 안됩니다] (http://meta.stackexchange.com/questions/19190/should-questions-include-tags- in-their-titles). – Romasz

답변

1

간단한 방법으로이를 구현할 수 있습니다. ListBoxItem ControlTemplate을 변경할 수 있습니다. 이 말은하여 PhoneApplicationPage의 자원 내에서 style을 넣을 수 있습니다 자세한 내용

코드입니다,이 당신을 도울 수있는 Selected VisualState

<Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="BorderBrush" Value="Transparent"/> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="HorizontalContentAlignment" Value="Left"/> 
     <Setter Property="VerticalContentAlignment" Value="Top"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBoxItem"> 
        <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="SelectionStates"> 
           <VisualState x:Name="Unselected"/> 
           <VisualState x:Name="Selected"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
              <!--The selected state, change the value to your color--> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Your Color"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

ListBox

<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle1}"/> 

소원을 변경합니다. 감사합니다

+0

좋은 좋은 직장 +1 –

관련 문제