2014-02-15 79 views
0

我有添加了樣式的列表框。更改列表框項目邊框顏色

這裏是我的代碼:

<!-- Style for list item selector --> 
     <Style x:Key="ListItemSelectorStyle" TargetType="ListBoxItem"> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="BorderThickness" Value="1" /> 
      <Setter Property="Padding" Value="0" /> 
      <Setter Property="HorizontalContentAlignment" Value="Center"/> 
      <Setter Property="VerticalContentAlignment" Value="Center"/> 
      <Setter Property ="Foreground" Value="Black" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBoxItem"> 
         <Border x:Name="ListBoxItem" Background="{TemplateBinding Background}" 
            HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
            VerticalAlignment="{TemplateBinding VerticalAlignment}" 
            BorderBrush="{TemplateBinding BorderBrush}" 
            BorderThickness="{TemplateBinding BorderThickness}"> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualState x:Name="Normal"/> 
            <VisualState x:Name="MouseOver"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ListItemBorder" Storyboard.TargetProperty="Background"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="#c9ebf2" /> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Disabled"/> 
           </VisualStateGroup> 
           <VisualStateGroup x:Name="SelectionStates"> 
            <VisualState x:Name="Unselected"/> 
            <VisualState x:Name="Selected"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ListItemBorder" Storyboard.TargetProperty="Background"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="#c9ebf2" /> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <Border x:Name="ListItemBorder" BorderBrush="Transparent" Background="#e3e8f0"> 
           <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> 
            <TextBlock 
              Name="textBlock" 
              Text="{Binding Path=answerText}" 
              HorizontalAlignment="Stretch" 
              Padding="10,25,10,25" 
              MinHeight="80" 
              VerticalAlignment="Center" 
              TextAlignment="Center" 
              Style="{StaticResource TextStyle}" 
              Foreground="Black"/> 
            <Image Name="ImageBlock" 
              Grid.Row="0" 
              Width="Auto" 
              Height="Auto" 
              Stretch="UniformToFill" 
              Source="{Binding answerImage}" 
              HorizontalAlignment="Center" 
              VerticalAlignment="Center" 
              Margin="1,1,1,1"/> 
           </Grid> 
          </Border> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

我的列表框:

<ListBox Name="listBox" 
           HorizontalAlignment="Stretch" 
           ItemContainerStyle="{StaticResource ListItemSelectorStyle}" 
           HorizontalContentAlignment="Stretch" 
           VerticalContentAlignment="Stretch" 
           SelectionChanged="ListBoxClicked" 
           ScrollViewer.VerticalScrollBarVisibility="Disabled"> 
          <ListBox.ItemTemplate> 
           <DataTemplate> 
</DataTemplate> 
          </ListBox.ItemTemplate> 
         </ListBox> 

在這裏,我已經添加了兩個顏色中的一種默認項顏色"#e3e8f0",並選擇其中一個項目,鼠標懸停顏色"#c9ebf2"在我樣式 現在我在.CS文件中有一個按鈕和它的單擊事件現在,當我單擊該選定項目和MouseOver顏色"#c9ebf2"應更改爲綠色,

如何實現這個目標?

回答

1

您在樣式中爲鼠標懸停和選定狀態提供了#c9ebf2顏色。改變你選擇的狀態的顏色。

+0

沒有好友,那不是問題,當我點擊一個按鈕,我需要改變列表框中單行的顏色,如何做到這一點? – Goofy

+0

當您在列表框中的某個項目上選擇一個項目或鼠標時,該項目將獲得'#c9ebf2'顏色。你在列表框外有一個按鈕。當你點擊按鈕時,所選擇的列表框的項目應該變成另一種顏色,這是你在找什麼? – stackamar

+0

是的,你是正確的 – Goofy