Showing posts with label DataForm. Show all posts
Showing posts with label DataForm. Show all posts

Sunday, September 25, 2011

Why is my Silverlight DataForm acting like that?

The other day, as I was using Silverlight, I put a DataGrid inside of a DataForm. Seems innocent enough, right? Specifically, what I wanted to accomplish was to allow users to add elements to a DataGrid with drag and drop functionality. Therefore, there were two DataGrids - one that the user dragged from, and the other that would receive the drop event. What I noticed, though, was that the added elements were not being saved.

We are using WCF RIA services, so I first assumed that I had created my composition objects incorrectly, and so I spent quite a bit of time looking into and verifying that code. After adding more and more breakpoints, I discovered that the WCF RIA service call was not to blame - the added elements were not even in the Submit operation, and so they were being removed sometime before the Submit was called.

Next, life got even more strange - if I edited one of the DataFields on the DataGrid, and then I dragged an item over, everything saved correctly. This left me quite puzzled. Then, eventually, a co-worker helped me discover the problem (which I thought was strange enough to warrant a blog post - maybe this will help other people (or even myself) next time one of us runs into this situation).

A Silverlight DataForm Rejects changes on EditEnded.

What? Yeah, I thought that this was quite strange. Why was I hitting this event? My "Save" button was not in the DataGrid! However, once you know this, it is easy enough to fix. Handle the EditEnding event on the DataForm and mark the event canceled:

private void DataForm_EditEnding(object sender, DataFormEditEndingEventArgs e)
{
       e.Cancel = true;
}

Done! Now my DataForm stopped rejecting my changes.

Tuesday, July 5, 2011

Making Values Required in a Silverlight DataForm

For those of you who have done validation in a Silverlight DataForm before, you probably are used to setting the [Required] attribute above all of the necessary fields in your WCF RIA project. From there, .NET will auto-magic the necessary code and, combined with lots of NotifyOnValidationError and ValidatesOnExceptions tags (and potentially some GetBindingExpression().UpdateSource()), viola! it will successfully perform validations. However, I ran into a situation where I was actually taking a couple of fields and transforming them into a single field from my WCF RIA service data object. After a long time of scratching my head, I was really stuck on trying to figure out how to force this validation to work. I had tried setting the [Required] attribute on the field they were bound to, added all the usual NotifyOnValidationError tags... nothing.

Eventually, I realized how to get around this issue (and it made me feel a bit silly for not coming up with this earlier.) You know where we set "ValidatesOnExceptions=true"... well... let's throw a ValidationException! So, in the setter of the property that I was binding the DataField to, if the data was invalid, I simply threw a ValidationException. Poof! It suddenly started working. Is there a better way to do this? Probably. However, this was able to get me back on the right path, and I was able to resume being productive.

Friday, April 1, 2011

Systematically Update Binding in Silverlight

So, after quite a bit of time in Silverlight, I have learned that DataForms are very powerful entities.  However, they like to hide from me what they are doing.  Therefore, when I get into a situation in which I need to simulate their behavior, this often ends with me banging my head against a wall and typing my query into Google in as many different ways as possible.  Recently, I have been attempting to force a validation on an object that was not in a DataForm... and I had no idea how.  Fortunately, I stumbled upon this forum and it helped me.  So that you don't have to bother reading it (that's why you're at my site, anyway, isn't it?) I have placed the important code snippet here:

BindingExpression bindingExpression = myTextBox.GetBindingExpresion(TextBox.TextProperty);
bindingExpression.UpdateSource();

Poof!  Your TextBox (assuming your TextBox variable is called "myTextBox") will validate.

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.

Wednesday, December 15, 2010

Silverlight DataForms with IsEditable

This may be something that everyone else knows already, but since this tripped me up for a little while, I figured I might as well post a blog about it.

Today, as I was tweaking one of our DataTemplates that was being used in a DataForm in Silverlight 4, I was running into a problem related to the IsEditable property.  I had a ComboBox in my template, and I tried everything I could to get it to be read-only (I needed it to be read-only only some of the time - depending on what was set in a different property).  After eventually thinking maybe my binding was broken, even though I saw it call the get of my property, I tried hard coding IsEnabled to false.  Still to no avail - it kept being editable (much to my chagrin).  Eventually, after reading about how DataForms worked, I came up with an idea - I put the IsEnabled binding on the DataField tag instead of on the ComboBox itself.  Sure enough, poof! it instantly started working and my ComboBox was enabled or disabled depending on the value that my DataField was bound to.

Hopefully this helps someone who runs into a similar problem.  As a sidenote, I also discovered during my testing that you can set something inside a DataForm to be read only by changing the Editable attribute on the property that is being bound to.  This wouldn't work in my situation, however, since whether the value was read only or not was dependent on the value of another property, so it made the attribute route more tricky.