How to get a callback from a DataBound property

   I'm writing this blog to show something that stumped me for about an hour, and hopefully this will save you some time.. or at least get you thinking creative!


  I have a user control in Silverlight, I'm using C# on the back end. The control's background color changes via viewmodel databinding. What I want to do, is get a callback when the background color changes. so I can start my storyboard which is a nice transition into the UI change of the control.

  Here's an idea of my control:


<Grid x:Name="LayoutRoot" Background="{Binding Background}" Height="85" Width="300" >


            <Storyboard x:Name="animate">

                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="LayoutRoot">

                    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>

                    <EasingDoubleKeyFrame KeyTime="0:0:1.0" Value="1"/>








   I spent a lot of time trying to do things like hook into the BIndingExpression and trying to wire up different ways to hook up to this control. Nothing, was, for lack of better terms, code pretty or really functional. I also really only care about when the background property on the Grid control changes. So how can this be accomplished? The answer is that you don't need to look up anything, you have all the knowledge to do this, you just have to think about it.


   The Grid control is a bad control for what we want.. it just doesn't give me what I need, a callback when the data (that again is Databound somewhere in a viewmodel) changes. But think about it.. other controls do have this functionality! The textbox control for one does! And that works perfectly for us. Let's add a textbox control to our user control:



<TextBox TextChanged="TextBox_TextChanged" Text="{Binding Background}" Opacity="0"/>



   The textbox control will now callback into the code anytime the Background (which in my case is a Solid Color brush) changes. Now all I have to do is wire up the event handler:



private void TextBox_TextChanged(object sender, TextChangedEventArgs e)





    So pretty easy to do, and the code is not freaky.. it's straight forward and consistent. It also allows me to wire up many textbox controls if necessary to monitor different property changes.