【问题标题】:WPF Animate Listbox Item Background ColorWPF 动画列表框项目背景颜色
【发布时间】:2014-05-14 16:13:51
【问题描述】:

我的代码接近完成我想要的,但我需要一些帮助。当我将鼠标悬停在单个列表框项目上时,我想从无背景颜色淡入到 0.5 不透明度橙色。这是我目前拥有的:

 <ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Control.Background" Value="Orange" />
                <Setter Property="Control.BorderBrush" Value="SteelBlue" />
                <Setter Property="Control.BorderThickness" Value="1" />
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
            Storyboard.TargetProperty="Opacity" To=".5"
            Duration="0:0:0.4"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
             Storyboard.TargetProperty="Opacity" To="0"
             Duration="0:0:0"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

但这会导致单个 ENTIRE 列表框项目在鼠标悬停时显示 0.5 不透明度橙色,然后在鼠标离开时完全消失。所以有两个问题:如何仅对每个列表框项的背景颜色属性进行动画处理,以及如何对动画进行排序以使其正常工作?

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:
    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>            
                <Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Margin" Value="1,2,1,1"/>
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                    <Setter Property="Background" Value="White" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Grid>
                                    <Border Background="{TemplateBinding Background}" />
                                   <Border Background="LightGray" Margin="0,0" Opacity="0.5">
                                    <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition />
                                                <RowDefinition />
                                            </Grid.RowDefinitions>
                                            <!--<Border Margin="2,1,2,0" Grid.Row="0" Background="#57FFFFFF" />-->
                                            <Border Margin="2,1,2,0" Grid.Row="0">
                                                <Border.Background >
                                                    <LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" >
    
                                                    </LinearGradientBrush>
                                                </Border.Background>
                                            </Border>
                                        </Grid>
                                    </Border>
                                    <ContentPresenter Margin="0,5" />
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition Property="IsMouseOver" Value="True" />
                                            <Condition Property="IsSelected" Value="False"/>
                                        </MultiTrigger.Conditions>
                                        <!--<Setter Property="Background" Value="#CCCBAF00" />
                                <Setter Property="Opacity" Value="0.8" />-->
                                        <Setter Property="Background">
                                            <Setter.Value>
                                                <LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" Opacity="0.8">
                                                <GradientStop Color="#F54F00" Offset="0" />
                                                <GradientStop Color="#F54F00" Offset="1" />
                                                </LinearGradientBrush>
                                            </Setter.Value>
                                        </Setter>
                                    </MultiTrigger>
                                    <Trigger Property="IsSelected" Value="True">
                                        <!--<Setter Property="Background" Value="#CCCB6400" />
                                <Setter Property="Opacity" Value="0.8" />-->
                                        <Setter Property="Background">
                                            <Setter.Value>
                                                <LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" Opacity="0.8">
                                                    <GradientStop Color="#CCCD8B4C" Offset="0" />
                                                    <GradientStop Color="#CCCB6400" Offset="1" />
                                                </LinearGradientBrush>
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>         
        </Window.Resources>
        <Grid>
              <ListBox Margin="20"  ItemContainerStyle="{StaticResource ListboxItemStyle}" >
              <ListBoxItem >New York</ListBoxItem>
              <ListBoxItem>Los Angeles</ListBoxItem>
              <ListBoxItem>Paris</ListBoxItem>
              <ListBoxItem>Zurich</ListBoxItem>
            </ListBox>
        </Grid>
    </Window>
    

    【讨论】:

      【解决方案2】:

      如果我把它放在 ListBox.Resources 而不是 ItemContainerStyle 上,你的 XAML 就可以工作

      <ListBox>
            <ListBox.Resources>
              <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="HorizontalContentAlignment"
                        Value="Stretch" />
                <Style.Triggers>
                  <Trigger Property="IsMouseOver"
                           Value="True">
                    <Setter Property="Control.Background"
                            Value="Orange" />
                    <Setter Property="Control.BorderBrush"
                            Value="SteelBlue" />
                    <Setter Property="Control.BorderThickness"
                            Value="1" />
                    <Trigger.EnterActions>
                      <BeginStoryboard>
                        <Storyboard>
                          <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                           To=".5"
                                           Duration="0:0:0.4" />
                        </Storyboard>
                      </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                      <BeginStoryboard>
                        <Storyboard>
                          <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                           To="10"
                                           Duration="0:0:0" />
                        </Storyboard>
                      </BeginStoryboard>
                    </Trigger.ExitActions>
                  </Trigger>
                </Style.Triggers>
      
              </Style>
            </ListBox.Resources>
      
          <ListBoxItem>AAA</ListBoxItem>
            <ListBoxItem>BBB</ListBoxItem>
            <ListBoxItem>CCC</ListBoxItem>
      
          </ListBox>
      

      【讨论】:

      • 它也像以前写的那样“工作”,只是没有达到预期的效果。在 ListBox.Resources 中包装 XAML 并不能解决我列出的任何一个问题。
      • @WaltRitsher - 将 ExitAction 中的 Storyboard To 更改为 100(返回可见)
      • @user3342256 - 他将Style 放在Listbox.Resources 中,因为对于这个小例子来说,这是一个最合乎逻辑的地方。第二:它工作。 Walt Ritsher 只需要照顾ExitAction,一切都会按照您的要求运行。请检查您是否没有任何附加的Storyboard 附加到Listbox 本身
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多