代码之家  ›  专栏  ›  技术社区  ›  eye_am_groot

添加时使新ListView项背景闪烁

  •  0
  • eye_am_groot  · 技术社区  · 7 年前

    正如标题所说,我正试图 ListViewItem 加载项目时背景颜色会发生变化。我能够改变不透明度(我的非常条纹化的XAML):

    <ListView Background="Black" ItemsSource="{Binding Somesource}" Drop="AddItem">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=OneWay}" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <Grid x:Name="SignalGrid">
                                <!-- Grid Information -->
                            </Grid>
                            <ControlTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=IsSelected}" Value="false">
                                    <Setter TargetName="SignalGrid" 
                                            Property="Background" 
                                            Value="Transparent"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
                                    <Setter TargetName="SignalGrid" 
                                            Property="Background" Value="Blue"/>
                                </DataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Loaded">
                        <BeginStoryboard>
                            <Storyboard AutoReverse="True" 
                                        RepeatBehavior="6x">
                                <DoubleAnimation Duration="0:0:0.3"
                                              Storyboard.TargetProperty="Opacity" 
                                              From="1.0" To="0.3"/>
                            </Storyboard>
                         </BeginStoryboard>
                     </EventTrigger>
                 </Style.Triggers>
             </Style>
         </ListView.ItemContainerStyle>
    </ListView>
    

    这工作很好,但不是真正的功能,我想。我试着做那件事 storyboard 比如:

    <Storyboard AutoReverse="True" RepeatBehavior="6x">
        <ColorAnimation Duration="0:0:0.3" 
                        Storyboard.TargetProperty="(ListViewItem.Background).(SolidColorBrush.Color)" To="Blue"/>
    </Storyboard>
    

    IsSelected DataTriggers 因为基本的想法是我想在 IsSelected = true IsSelected = false Storyboard.TargetProperty="(ListViewItem.Background).(SolidColorBrush.Color)" 我肯定我错过了一些简单的东西,但任何帮助都将不胜感激。

    我找到了解决办法。在 <!-- Grid Information --> this solution

    3 回复  |  直到 7 年前
        1
  •  1
  •   mm8    7 年前

    ListViewItem 加载时闪烁:

    <ListView ItemsSource="{Binding Somesource}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=OneWay}" />
                <Setter Property="Background" Value="Transparent" />
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Loaded">
                        <BeginStoryboard>
                            <Storyboard AutoReverse="True" RepeatBehavior="6x">
                                <ColorAnimation Duration="0:0:0.3" 
                                                Storyboard.TargetProperty="(ListViewItem.Background).(SolidColorBrush.Color)" 
                                                To="Blue"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
    
        2
  •  1
  •   Ilya Grigoryan    7 年前
    <ListView Background="Black" ItemsSource="{Binding Somesource}" Drop="AddItem">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=OneWay}" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <Grid x:Name="SignalGrid"  Background="{TemplateBinding Background}">
              <!-- Grid Information -->
        </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Loaded">
                        <BeginStoryboard>
                            <Storyboard AutoReverse="True" RepeatBehavior="6x">
        <ColorAnimation Duration="0:0:0.3" 
                        Storyboard.TargetProperty="(ListViewItem.Background).(SolidColorBrush.Color)" To="Blue"/>
    </Storyboard>
                         </BeginStoryboard>
                     </EventTrigger>
                 </Style.Triggers>
             </Style>
         </ListView.ItemContainerStyle>
    </ListView>
    
        3
  •  0
  •   eye_am_groot    7 年前

    就为了结束这个问题。如“编辑”中所述 <!-- Grid Information --> ,我有一个 border ,并将其更改为:

    <Border>
        <Border.Background>
            <SolidColorBrush Color="Black" x:Name="RowBackground"/>
        </Border.Background>
    </Border>
    

    然后,仍然在 ControlTemplate

    <ControlTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=IsSelected}" Value="false">
            <Setter TargetName="SignalGrid" Property="Background" Value="Transparent"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
            <Setter TargetName="SignalGrid" Property="Background" Value="Blue"/>
        </DataTrigger>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard AutoReverse="True" RepeatBehavior="6x">
                    <ColorAnimation Duration="0:0:0.5" Storyboard.TargetName="RowBackground" 
                                    Storyboard.TargetProperty="Color" To="Blue"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </ControlTemplate.Triggers>