Workarounds for dataform bugs

by StefanOlson 6. August 2009 09:29

Unfortunately the dataform that was shipped with Silverlight 3 RTW (in the Toolkit) was massively changed from the beta version, and as a result has a number of bugs that were not there during the beta.

To find these bugs create a new project using the business application template, and run it.  Click login, type some text into Username and then click OK and you will see the following screen:

image You will note that there are two problems with this screen:

1. the first one is that the data form has become opaque

2. the second one is the fact that the password and confirm password, which are required fields are not marked in red.

3. If you add in a checkbox as a label and click OK, you get this screen:

image

Workarounds

1. This bug is caused because the DataForm doesn’t listen for IsEnabledChanged, but in the UpdateStates() function will call GoToState to change for the disabled state. Unfortunately if it doesn't go back and to update states after if you enable, you're left in the disabled state. The workaround is to create a derived class that looks like this and use it in place of the dataform:

public class LessBuggyDataForm:DataForm
   {
       private const string DATAFORM_stateDisabled = "Disabled";
       private const string DATAFORM_stateNormal = "Normal";

       public override void OnApplyTemplate()
       {
           IsEnabledChanged += LessBuggyDataForm_IsEnabledChanged;
           base.OnApplyTemplate();
       }

       void LessBuggyDataForm_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
       {
           if (!IsEnabled)
           {
               VisualStateManager.GoToState(this, DATAFORM_stateDisabled, true);
           }
           else
           {
               VisualStateManager.GoToState(this, DATAFORM_stateNormal, true);
           }
       }
   }

 

2.  I'm not entirely sure why this bug is caused. What happens is that unless the password text is updated, the validation doesn't seem to occur. Therefore, the workaround is every time you want to force validation to happen. You need to call a function like this in the password control:

public void UpdatePasswordText()
        {
            this.PasswordText = this.password.Password;
        }

Normally you'd call this before you call ValidateItem() or CommitEdit()

3. This problem is caused by the fact that when you click the okay button, CancelEdit() is called, which completely rebuilds the label. The exception is because checkbox is already the child of another control, and it tries to make it a child of the new label.

The workaround is to stop the ability to cancel the editing like this:

registerForm.EditEnding += new EventHandler<DataFormEditEndingEventArgs>(registerForm_EditEnding);
}

void registerForm_EditEnding(object sender, DataFormEditEndingEventArgs e)
{
    if (e.EditAction==DataFormEditAction.Cancel)
    {
        e.Cancel = true;
        return;
    }
}

Hopefully these solutions will save someone else some time.

…Stefan

Tags:

Silverlight

Comments

9/14/2009 1:45:56 PM #

Hi Stefan,

Great post, and yes it has definitely saved me a ton of time.  I have another bug to report as well, which occurs when there is a ComboBox inside the data form.  You can load up the ComboBox with items through binding no problem, and even get the SelectedItem or SelectedIndex binding going no problem, but this issue is that after binding occurs, the ComboBox remains disabled.  If you add an OnLoaded handler to such a ComboBox, all you need to do is set IsEnabled = true, and all is well again.

private void TitleComboBox_Loaded(object sender, RoutedEventArgs e) {
   // *** HACK ***
   // Fixes a binding issue with a combobox inside a dataform
   var combo = sender as ComboBox;
   if (combo != null) {
      combo.IsEnabled = true;
   }
}

David Judd

9/15/2009 9:15:32 AM #

David,

Thanks for that!

...Stefan

StefanOlson

10/5/2009 3:11:20 AM #

Thanks for posting the workaround on the initially disabled dataform.  Saved me a ton of head-banging!

wtfChris

2/6/2010 1:35:56 PM #

Thank you, number 1 was very helpful and I am using this.

Number 2, I have this issue, but I don't quite understand the fix.  Where is this code supposed to go?  I don't understand the PasswordText and .password.Password references.   I am trying to get this to work in the Navigation Template of VS2008 which has the LoginWindow control.

By the way, I took a look at the Virutal Castle, very interesting! What's the deal with the plane in the background at the castle?  Is that for security purposes?

Greg

Greg

6/12/2010 12:46:21 PM #

One thing when fixing controls with derived classes is you lose theming on the controls...
Got to say this BusyIndicator or the Dataform one or the other needs to be fixed this issue is in Silverlight 4 as well and thats sad.

razor24

6/12/2010 1:25:28 PM #

I made the code a behavior instead of a derived class and all is well now thanks again for this fix...
This is for Silverlight 4 as well....it still has the bug

razor24

Add comment




biuquote
  • Comment
  • Preview
Loading



About the author

Stefan Olson is the Managing Director of Olson Software.  He has been developing software using Microsoft Technologies for nearly 20 years.

He is currently working on building the next generation Virtual Tour software in WPF and Silverlight for www.palacevirtualtours.com.

Tag cloud