2012-06-15 102 views
0

我一直在爲我的列表框工作,但我遇到了一個問題。當我將鼠標懸停在列表框中的任何項目上時,我的動畫將在所有項目上運行,而不僅僅是我懸停的項目。我一直在尋找互聯網,但似乎有一堆不同的方式來做到這一點,但他們中沒有一個能夠解決這個問題。WPF ListBoxItem風格動畫

<Color x:Key="BorderColor" A="255" R="216" G="217" B="220" /> 
<Color x:Key="ForegroundColor" A="255" R="40" G="40" B="40" /> 
<Color x:Key="BackgroundColor" A="255" R="240" G="241" B="241" /> 

<SolidColorBrush x:Key="BorderBrush" Color="{StaticResource BorderColor}" /> 
<SolidColorBrush x:Key="ForegroundBrush" Color="{StaticResource ForegroundColor}" />   
<SolidColorBrush x:Key="BackgroundBrush" Color="{StaticResource BackgroundColor}" /> 

<Color x:Key="Color_Red" A="255" R="235" G="103" B="103" /> 

<Style x:Key="Test" TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="Foreground" Value="{StaticResource ForegroundBrush}" /> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <Border x:Name="Border" Background="{StaticResource BackgroundBrush}" BorderBrush="{StaticResource BorderBrush}" BorderThickness="0,0,0,3" CornerRadius="0" Margin="0,0,2,3" Padding="0" SnapsToDevicePixels="true"> 
        <Grid Height="Auto" Width="Auto"> 
         <ContentPresenter Margin="3,3,0,0" VerticalAlignment="Top" /> 
        </Grid> 
       </Border> 
       <ControlTemplate.Triggers> 
        <EventTrigger RoutedEvent="ListBoxItem.MouseEnter"> 
         <EventTrigger.Actions> 
          <BeginStoryboard> 
           <Storyboard> 
            <ColorAnimation Duration="0:0:0.5" 
                Storyboard.TargetName="Border" 
                Storyboard.TargetProperty="BorderBrush.Color" To="{StaticResource Color_Red}" /> 
           </Storyboard> 
          </BeginStoryboard> 
         </EventTrigger.Actions> 
        </EventTrigger> 
        <EventTrigger RoutedEvent="ListBoxItem.MouseLeave"> 
         <EventTrigger.Actions> 
          <BeginStoryboard> 
           <Storyboard FillBehavior="Stop"> 
            <ColorAnimation Duration="0:0:0.3" 
                Storyboard.TargetName="Border" 
                Storyboard.TargetProperty="BorderBrush.Color" To="{StaticResource BorderColor}" /> 
           </Storyboard> 
          </BeginStoryboard> 
         </EventTrigger.Actions> 
        </EventTrigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

任何幫助將不勝感激,我一直停留在這一段時間...

謝謝!

回答

1

關閉我的頭頂我認爲你的動畫資源,而不是隻是邊框顏色,嘗試更改邊界聲明。

<Border x:Name="Border" Background="{StaticResource BackgroundBrush}" BorderThickness="0,0,0,3" CornerRadius="0" Margin="0,0,2,3" Padding="0" SnapsToDevicePixels="true"> 
    <Border.Style> 
     <Style TargetType="{x:Type Border}"> 
      <Setter Property="BorderBrush" Value="{StaticResource BorderBrush}"/> 
     </Style> 
    </Border.Style> 
    <Grid Height="Auto" Width="Auto"> 
     <ContentPresenter Margin="3,3,0,0" VerticalAlignment="Top" /> 
    </Grid> 
</Border> 

基本上從邊界線中刪除邊界刷並將其放置在下面的樣式設置器中。

+0

這就是我的想法,但我不知道如何解決它。我仍然不明白爲什麼這個工作,但它的確如此。謝謝! – Lukasz