【问题标题】:How to flash item in ListBox when property changes属性更改时如何在 ListBox 中闪烁项目
【发布时间】:2014-03-25 09:40:32
【问题描述】:

我有一个ListView,它绑定到以下类型的项目List

class Item : INotifyPropertyChanged
{
    //Both these props have property change notification implemented. Left out for brevity.
    int Price{get;set}
    int Size{get;set}
}

我有一个 DataTemplate 让这个课程显示在 ListView

列表的大小不会改变,但列表中项目的属性值会改变。假设我首先有三个这样的项目:

Item format : Size@Price

{(10@34), (15@37), (10@38)}

假设现在第一项仅因数据更新而改变:

{(15@34), (15@37), (10@38)}

请注意,我不会删除现有项目。我只改变它的属性。因此,每次更改都会触发属性更改通知。我想突出显示/闪烁在ListView 中更改的项目。我该怎么做(最好在 XAML 中)?

【问题讨论】:

    标签: wpf inotifypropertychanged


    【解决方案1】:

    不确定我是否正确,您是否只想在SizePrice 属性更改时闪现ListViewItem

    如果是这样,您应该可以使用Binding.TargetUpdated 事件触发器来做到这一点。您确实需要确保在其绑定中对 SizePrice 的绑定也指定 NotifyOnTargetUpdated=True 以触发此事件。

    所以这样的事情应该可以正常工作:

    <DataTemplate x:Key="SomeTemplate" DataType="viewModel:Item">
      <StackPanel x:Name="stackPanel"
                  Background="Transparent"
                  Orientation="Vertical">
        <!-- This prolly aint your layout. Just here as an example -->
        <TextBlock Text="{Binding Size, NotifyOnTargetUpdated=True}" />
        <TextBlock Text="{Binding Price, NotifyOnTargetUpdated=True}" />
      </StackPanel>
      <DataTemplate.Triggers>
        <EventTrigger RoutedEvent="Binding.TargetUpdated">
          <BeginStoryboard>
            <Storyboard AutoReverse="True">
              <!-- From="1.0" helps prevent spam property changes skewing the opacity -->
              <DoubleAnimation Duration="0:0:0.3"
                               Storyboard.TargetName="stackPanel"
                               Storyboard.TargetProperty="Opacity"
                               From="1.0"
                               To="0.3" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
    

    替代:

    来自 blend with xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 您还应该拥有&lt;ei:PropertyChangedTrigger&gt;,然后您可以使用它调用行为来触发Storyboard&lt;ei:ControlStoryboardAction&gt;。我个人只使用了第一种方法,因为它稍微简单一些。

    【讨论】:

    • 我会试试这个!谢谢。
    • @nakiya 你真的试过了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多