DependencyObject.InvalidateProperty not working

后端 未结 3 2007
暖寄归人
暖寄归人 2021-01-11 12:16

Based on the documentation via MSDN...

You can also use InvalidateProperty to force re-evaluation of a binding against a data source that is not a

3条回答
  •  走了就别回头了
    2021-01-11 12:51

    Yes. I have an idea.

    The reason why your code doesn't work is that button asks for new value, but Binding object holds the old one as it hasn't got PropertyChanged notification. The changes chain in standart scenario looks like:

    Payload.Timestamp -> Binding object -> Button.ContentProperty

    In your scenario, when you call InvalidateProperty chain is:

    Binding object -> Button.ContentProperty

    So, you should notify binding object that its source has been changed with next code:

        private void Invalidate(object sender, RoutedEventArgs e)
        {
            _payload.Timestamp = DateTime.Now.Add(TimeSpan.FromHours(1)).ToLongTimeString();
    
            Button b = sender as Button;
            BindingExpression be = b.GetBindingExpression(Button.ContentProperty);
            be.UpdateTarget();
        }
    

    As you can see, I even shouldn't call InvalidateProperty, because the Binding mechanizm due to changed underlying source will automatically force Button to refresh content.

提交回复
热议问题