ForcingPropertyChangedCallback of a DP to fire

111 views Asked by At

My question might sound a bit similar to this question

I have a Notification bar that undergoes certain Animations when a DependencyProperty of my custom UserControl changes. Here goes the code implmentation:

    public string StatusBarText
    {
      get { return (string)GetValue(StatusBarTextProperty); }
      set { SetValue(StatusBarTextProperty, value); }
    }

   public static readonly DependencyProperty StatusBarTextProperty =
        DependencyProperty.Register("StatusBarText", typeof(string), typeof(WorkspaceFrame), new FrameworkPropertyMetadata(null, StatusBarTextChangedCallBack));

    private static void StatusBarTextChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      if (!string.IsNullOrWhiteSpace(Convert.ToString(e.NewValue)))
      {
        var workspaceFrame = d as WorkspaceFrame;
        if (null == workspaceFrame)
          return;
        var storyboard = workspaceFrame.FindResource("notificationBarAnimation") as Storyboard;
        if (null == storyboard)
          return;
        var statusBar = workspaceFrame.Template.FindName("PART_StatusBar", workspaceFrame) as Border;
        if (null == statusBar)
          return;
        //Run the animation
        storyboard.Begin(statusBar);
      }
    }

and here is the Border that is getting animated:

<Border x:Name="PART_StatusBar" Margin="5" BorderThickness="2" VerticalAlignment="Top"
                    DataContext="{Binding Path=StatusText}" Opacity="0"
                    Visibility="{Binding Path=Opacity, Mode=OneWay, RelativeSource={RelativeSource Self}, Converter={StaticResource doubleToVis}}"
                    BorderBrush="{StaticResource StatusMessageBackBrush}">

                <Border.Background>
                  <SolidColorBrush Color="{StaticResource StatusMessageBackColor}" Opacity="0.7"/>
                </Border.Background>

                <TextBlock Margin="10" FontSize="17" Foreground="{StaticResource BlackColorBrush}" Text="{Binding}">

                </TextBlock>
              </Border>

As it might be clear to you by now, that this DP is bound to a VM property which fires the PropertyChangedNotification (of INotifyProeprtyChanged) when set. Now the problem is the StatusBarTextChangedCallBack is called only if there is some change in the Old and New value of the DP. Is there a way to force it to run? If not, is there some way around? I need to show the same notification over and over. And the animation should fire.

regards,

James

2

There are 2 answers

2
Nitin On BEST ANSWER

You can register a CoerceValueCallBack function instead of ValueChanged. Something like this:

public static readonly DependencyProperty StatusBarTextProperty =
        DependencyProperty.Register("StatusBarText", typeof(string), typeof(WorkspaceFrame), new FrameworkPropertyMetadata(null,null, StatusBarTextCoerceValueCallBack));



private static object StatusBarTextCoerceValueCallBack(DependencyObject d, object value)
{

 }

CoerceValueCallback will always be triggered when the value of a property changes even if the value is same.

Thanks

1
Sheridan On

The normal way to achieve what you want (or calling an event handler manually) is to create a method to start your animations. Then call that animation starting method from inside your property changed handler. This way, you can simply call this method whenever you need to.