【问题标题】:WPF Animation not getting triggered when property value remains same属性值保持不变时未触发 WPF 动画
【发布时间】:2014-05-14 11:04:01
【问题描述】:

我遵循了这个问题中描述的方法:Highlighting cells in WPF DataGrid when the bound value changes

<Style x:Key="ChangedCellStyle" TargetType="DataGridCell">
    <Style.Triggers>
        <EventTrigger RoutedEvent="Binding.TargetUpdated">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Duration="00:00:15"
                        Storyboard.TargetProperty=
                            "(DataGridCell.Background).(SolidColorBrush.Color)" 
                        From="Yellow" To="Transparent" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>

<DataGridTextColumn Header="Status" 
    Binding="{Binding Path=Status, NotifyOnTargetUpdated=True}" 
    CellStyle="{StaticResource ChangedCellStyle}" />

我面临的问题是,当底层属性值没有改变时,动画没有被触发。在上面给出的例子中,如果“Status”属性的值没有改变,那么动画就不会被触发。有没有办法,不管值有没有变化,我都可以触发动画。

谢谢。

【问题讨论】:

    标签: c# wpf wpfdatagrid


    【解决方案1】:

    我的猜测是,当值没有改变时,你实际上并没有改变你的虚拟机中的属性值。这是 MVVM 中非常常见的行为,即在真正需要时不引发属性更改,但在您的情况下,无论值是否更改,您都希望引发属性更改事件。

    所以如果你有类似的东西:

    public string Status {
      get { return _status; }
    
      set {
        if (_status == value)
        {
          return;
        }
        _status = value;
        RaisePropertyChanged(() => Status);
      }
    }
    

    改成:

    public string Status {
      get { return _status; }
    
      set {
        //if (_status == value)
        //{
        //  return;
        //}
        _status = value;
    
        // Following line is the key bit. When this(property-changed event) is raised, your animation should start.
        // So whenever you need your animation to run, you need this line to execute either via this property's setter or elsewhere by directly raising it
        RaisePropertyChanged(() => Status);
      }
    }
    

    这将在每次调用属性的设置器时触发属性更改事件,然后无论值是否更改,都会触发您的动画。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2020-03-25
      • 2011-02-13
      • 2018-04-16
      • 2013-02-16
      • 2011-02-24
      • 2011-10-04
      相关资源
      最近更新 更多