Thursday, February 17, 2011

Silverlight Data Forms with BusyIndicator

So, I'm sure that this has been posted elsewhere, but I don't really remember where, so I figured I would re-post it if for no other reason than it would allow me to find it again.


When using Silverlight 4 with the toolkit, I ran into an interesting bug.  Sometimes when I would use a DataForm along with the toolkit's BusyIndicator, the DataForm would appear to be disabled, even after the BusyIndicator was no longer showing.  (To see more about the bug itself, you can go here.)  After discovering what the issue was, I researched on Google how I should fix this.  As it turns out, it is a pretty simple fix. I simply added the following (note that DataForm is the variable name of the DataForm in my XAML):

DataForm.IsEnabledChanged += new DependencyPropertyChangedEventHandler(DataForm_IsEnabledChanged);

and

        void DataForm_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (!IsEnabled)
            {
                VisualStateManager.GoToState(DataForm, "Disabled", true);
            }
            else
            {
                VisualStateManager.GoToState(DataForm, "Normal", true);
            }
        }


Poof!  The code was able to go back to the correct state.  Hopefully Microsoft will fix this so that this workaround is no longer required, but in the meantime, I figured it might help someone to see this.