×

First time here?

You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting!

If you looked at the updated RIA Services Business Application template which had the authentication built-in, you may have seen the login screen with a little icon next to the password field:

Biz App Login Template

When you hover over this some helpful information displays in tool tip form:

Tooltip Helper

So how did that get there?  The power of the data annotations.  If you look at your model definition, you can add a DisplayAttribute and provide some additional information.  Let’s take a look at a simple example.  Here’s the simple model:

   1: public class PersonModel
   2: {
   3:     public string FirstName { get; set; }
   4:     public string LastName { get; set; }
   5:     public string EmailAddress { get; set; }
   6:     public string Gender { get; set; }
   7:     public int Age { get; set; }
   8: }

and the DataForm generated:

DataForm with no Attributes

Not too helpful.  Now, let’s modify our model with some attributes:

   1: public class PersonModel
   2: {
   3:     [Required()]
   4:     [Display(Name="First Name:")]
   5:     public string FirstName { get; set; }
   6:     
   7:     [Required()]
   8:     [Display(Name = "Last Name:")]
   9:     public string LastName { get; set; }
  10:  
  11:     [Display(Name = "Email Address:", 
  12:         Description="We do not sell your information!")]
  13:     public string EmailAddress { get; set; }
  14:  
  15:     [Display(Description="Used for demographics")]
  16:     public string Gender { get; set; }
  17:  
  18:     public int Age { get; set; }
  19: }

And here is the new auto generated DataForm:

DataForm with Display Attributes

Much more friendly to the user, and from a code perspective, we wouldn’t have to change how we work with our model.  We get some free visuals and functionality with some simple attribute properties.

Also, did you know that you could bind multiple items to the DataForm and get automatic paging and add new functionality?  Given this code:

   1: ObservableCollection<PersonModel> people = new ObservableCollection<PersonModel>();
   2: for (int i = 0; i < 10; i++)
   3: {
   4:     PersonModel p = new PersonModel() { FirstName = "First" + i.ToString(), LastName = "Last " + i.ToString(), Age = i };
   5:     people.Add(p);
   6: }
   7:  
   8: DataBrowser.ItemsSource = people;

Check out what is generated:

DataForm multiple data binding

Notice the pager and add new buttons.  Nice.  If you’re wondering how to get more granular control over the field displays, it is similar to DataGrid in that you can turn off auto generation of fields and provide your own implementation through custom DataFields:

   1: <datacontrols:DataForm x:Name="DataBrowser" Width="400" AutoGenerateFields="False">
   2:     <datacontrols:DataForm.Fields>
   3:         <datacontrols:DataFormTextField Binding="{Binding FirstName}" 
   4:                 FieldLabelContent="First Name: " />
   5:         <datacontrols:DataFormTextField Binding="{Binding LastName}" 
   6:                 FieldLabelContent="Last Name: " />
   7:         <datacontrols:DataFormTemplateField FieldLabelContent="Age">
   8:             <datacontrols:DataFormTemplateField.DisplayTemplate>
   9:                 <DataTemplate>
  10:                     <TextBlock Text="{Binding Age}" FontSize="24" />
  11:                 </DataTemplate>       
  12:             </datacontrols:DataFormTemplateField.DisplayTemplate>
  13:             <datacontrols:DataFormTemplateField.EditTemplate>
  14:                 <DataTemplate>
  15:                     <TextBox Text="{Binding Age}" />
  16:                 </DataTemplate>
  17:             </datacontrols:DataFormTemplateField.EditTemplate>
  18:         </datacontrols:DataFormTemplateField>       
  19:     </datacontrols:DataForm.Fields>
  20: </datacontrols:DataForm>

Hope this helps!


This work is licensed under a Creative Commons Attribution By license.


5/25/2009 1:31 PM | # re: Silverlight DataForm helpers
Very nice - I've been using the data annotations with asp.net mvc (Steve Sanderson's xVal library) - fantastic stuff :)
5/25/2009 4:08 PM | # re: Silverlight DataForm helpers
Very easy and helpful.
5/25/2009 9:56 PM | # re: Silverlight DataForm helpers
That's a nice one!
5/25/2009 11:18 PM | # re: Silverlight DataForm helpers
Very nice and easy tutorial!
5/25/2009 11:40 PM | # re: Silverlight DataForm helpers
Dear Tim.

First, lots of thanks for the your great blog posts. Now about ria services are a great feature in SL3, but how about realtional data and for example lazzy load, could explain more this escenario,i mean if i have a product and a category table and in a data form i want see the category but no the id i prefer a combobox filled whit category objects and if my model is in EF how i can do it?? with linq to sql i coul do it whit loadoptions, but whit EF i cant find the way could you helpme ????

thanks a lot for your time and sorry 'cause my english is not very goodlooking :P

Zeze Lazo
5/26/2009 1:13 AM | # re: Silverlight DataForm helpers
Tim,
What about multicolumn DataForm? It would be nice to have it out of the box.
5/26/2009 7:50 AM | # re: Silverlight DataForm helpers
Tim, thanks for this great blog post.
Rachida
5/26/2009 1:05 PM | # re: Silverlight DataForm helpers
very nice. But I was wondering will it work with globalization and resource files. Or is that something in the works?

britto
5/26/2009 2:18 PM | # re: Silverlight DataForm helpers
It's indeed way closer to perfection :) But I have a little concern about translating the labels into a different language. From what I've seen in the code, the way to do it would be to turn off auto generation and then bind FieldLabelContent property to a data store which is aware of the current language in the app and would provide the "right" translation. But it would be really nice if the framework itself would provide the placeholders to declaratively handle the translations. If it's not yet there (sorry but don't know much yet about the SL3 forms support), then would very much like to add this as a feature request.
5/27/2009 1:01 AM | # re: Silverlight DataForm helpers
Very nice. A beginner question: Can we dynamically modify the value of these attributes (ex: foo in [Display(Name = "foo")]) at runtime ?
5/27/2009 1:45 AM | # re: Silverlight DataForm helpers
It's great to read your blog.I just want you to do me a favour.I want to bind dataform values to sql server 2005 database but could not get the values of the textbox using x:Name.I would be happy if you could help me out.
Thanking you in advance.
5/27/2009 1:52 AM | # re: Silverlight DataForm helpers
A little bit more on the DataForm up in the video here;

silverlight.net/learn/learnvideo.aspx?video=187317
5/27/2009 9:17 AM | # re: Silverlight DataForm helpers
Marc - not attributes at runtime, but you can always add DataField programmatically to the DataForm.

babina - you'll have to provide more information.

Mike -- yes, forgot to add your vid as a reference which goes into a lot of these!
6/1/2009 4:25 PM | # re: Silverlight DataForm helpers
How can i change the properties of the password that are needed for the Registration, i mean: minlength 7, 1 non alphanumeric character, i want that a non alphanumeric cahracter is not needed in the password, but how can i change it?

P.S.:Sorry for the worst english.
6/1/2009 4:54 PM | # re: Silverlight DataForm helpers
Alexander -- good question. I'd recommend a RegularExpression annotation...take a look at this regex to use: www.zorched.net/.../password-strength-validatio...
6/1/2009 9:13 PM | # re: Silverlight DataForm helpers
Tim - I'm experiencing the same problem reported in this link:

http://silverlight.net/forums/t/92594.aspx

As put by 'Enrai':

"When I try to bind the FieldLabelContent property of a DataForm's Field, I get the following error: AG_E_PARSER_BAD_PROPERTY_VALUE. Does this mean that I can't bind this property of the FieldControls?

The reason I want to bind this property is that I have the values of my FieldLabel's in a resource file."

In my case I need to translate the displayed label for the field depending on the UICulture of the user (I'm using a Converter in a binding).

Is this a bug, a feature, or likely pilot error on my part?

Thanx!



6/5/2009 7:25 AM | # re: Silverlight DataForm helpers
6/9/2009 6:41 PM | # re: display of info icon help when used in tempplate field
I am having problems to force displaying of the “little info icon” next to the text field when I was placing the TextBox within the DataFormTemplateField. I was making experiments with more granular control over the horizontal placement of the text boxes in a DataForm and I had the following configuration.

<dataForm:DataFormTemplateField >
<dataForm:DataFormTemplateField.DisplayTemplate >
<DataTemplate >
<StackPanel HorizontalAlignment="Right">
<TextBox Text="{Binding Rate}" HorizontalAlignment="Right" />
</StackPanel>
</DataTemplate>
</dataForm:DataFormTemplateField.DisplayTemplate>
<dataForm:DataFormTemplateField.EditTemplate>
<DataTemplate >
<StackPanel HorizontalAlignment="Right">
<TextBox Text="{Binding Rate}" HorizontalAlignment="Right" />
</StackPanel>
</DataTemplate>
</dataForm:DataFormTemplateField.EditTemplate>
</dataForm:DataFormTemplateField>

Where the “Rate” is annotated property with a Display attribute. In this configuration I expected to have rendered the label (before the textbox) and little help icon after the textbox but they simply were missing until I decided to remove the StackPanel controls.

I also tried to use the following

<StackPanel Orientation="Horizontal">
<dataForm:FieldLabel Target="{Binding ElementName=lbRate}" />
<TextBox x:Name="lbRate" Text="{Binding Rate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}"/>
<dataForm:DescriptionViewer Target="{Binding ElementName=lbRate}" />
</StackPanel>
Within the DataTemplate tags but again, only the textbox was rendered.
Am I missing something here?
7/14/2009 1:33 AM | # re: Silverlight DataForm helpers
Hi Tim
When I try to implement DataFormTemplateField.DisplayTemplate I get an error 'DisplayTemplate does not exist on the type DataForm in the xmlns Windows.Controls.Data.DataForm.Toolkit'. What am I doing wrong?
7/15/2009 3:29 AM | # Re: DataForm
I would like to know if there is a way in Dataform to specify the control (my own control instead of standard textbox control), that would be created dynamically for each of the property.
7/15/2009 5:13 AM | # re: Silverlight DataForm helpers
Hi Tim

Are there any examples of databinding combobox for foreign key relationships?
Gravatar
7/15/2009 3:59 PM | # re: Silverlight DataForm helpers
how to HIDE or SHOW a field based on authentication roles?
7/17/2009 5:33 AM | # re: Silverlight DataForm helpers
Hi, i have a question regarding the access of datafield content when the form is generated automatically:

im just playing with the dataform so don't ask WHY i actually want to do this but im trying to add a dataform with the Customer table (adventureworks) as itemssource and autogenerate set to "true", which worked perfectly. then, below i have a textbox and a button. when i hit the button i want to get the lastname of the currently selected record to be displayed in the textbox. but how can i actually get the currently selected customerid (primary key) from the dataform when the datafields are generated automatically? Is that possible? Thanks in advance!
7/17/2009 6:00 AM | # re: Silverlight DataForm helpers
Illob -- you can get the CurrfentItem from the data form which would represent your customer object, then get the FirstName property from the object.
7/21/2009 12:31 AM | # re: Silverlight DataForm helpers
Thanks, that helped =)
10/21/2009 6:58 AM | # re: Silverlight DataForm helpers
Hi Tim,

I cannot help but notice this example makes use of only textboxes, not very practical in reality. Could you please post a link (if you know of one) to a more comprehensive example of a LOB app which for example makes use of this DataForm,.Net Ria Services and Linq2Sql implementing a EditItemTemplate with comboboxes and two way binding implemented all the way through to persisting it to the database?

I love the DataForm control but I have yet to come across a comprehensive example of how to implement it in the real world!
11/14/2009 8:20 AM | # re: Silverlight DataForm helpers
Hi Tim

I am developing a Portal in Silverlight 3. I noticed that the default login screen as shown at the top of this page doesn't force the password and confirm password fields to match. In fact, the confirm password field isn't required.

I have changed the confirm password so that it is required and I have added a Custom Validation on the confirm password field to ensure that it matches the password.

The problem I have is that if they don't match, the error message I want to return is not shown next to the confirm password field. The label changes to red but the message doesn't appear.

If I amend the login form to use a TextBox for the password, instead of the PasswordControl then the error message I return from the custom validation shows when the user clicks into the field.

Is there a way to get the error message to appear when using the PasswordControl.
11/26/2009 3:42 PM | # re: Silverlight DataForm helpers
Great tutorial,

How can i change the default type of controls used in this example? For example, how can i change the gender field to use a dropdown control containing the user friendly values of "male' and "female", instead of the textbox in this example? Or how can i change a text field to use ,for example, a telerik custom control?

thanks
11/26/2009 11:06 PM | # re: Silverlight DataForm helpers
femi - DataForm by default determines fields based on data types -- but you can put your own DataForm.EditTemplates in -- look at the properties of DataForm on how to specify your own fields.
Gravatar
11/27/2009 2:51 PM | # re: Silverlight DataForm helpers
How can we access the DataFrom.EditTemplate fields? On Edit Ending, i want to check if the value entered already exists the datasource.dataview, however it Im not able to reference the fileds within the DataForm.EditTemplate
3/20/2010 4:24 PM | # re: Silverlight DataForm helpers
I need to bind a complex class to a dataform. The class members are as follows:
int MemberId
string MemberName
bool Active
Dictionary<string,string> Attributes
I need to bind the class to the dataform. My issue is that when I bind the class as is I get one field for the dictionary. How do I bind the entire class with the keys being the field name and values as the field values. If I put it all of the members in one dictionary the dictionary will bind properly but each entry is a different record and It will not allow me to update the value. I can work with the dictionary as the binding object but need to know how to display all the fields on one as one record.
The Attribute field is dynamic in nature due to not all members will have all attribute entries.
Any suggestions would be appreciated.
Thanks
Roby
4/13/2010 5:35 AM | # re: Silverlight DataForm helpers
How do I customize the buttons DataForm? I have to change the icons of DataForm for custom icons.
4/28/2010 10:37 PM | # re: Silverlight DataForm helpers
Can we have same thing without dataform

actully i want in simale validation and description in isolate textbox which is not related to any dataform.

Please reply if any of you find the way to do this.
6/30/2010 8:44 PM | # re: Silverlight DataForm helpers
mik0, I have the exact same problem. Does anyone know how to fix it?
8/1/2010 10:03 PM | # re: Silverlight DataForm helpers
Hi, Thanks for Valuable Information.

I have just applied in my project,but i stuck when i want to hanle LostFocus Event(or any event) of AutoGeneratedField(NoOfGoods) in Dataform.

Description

I have 4 Fields. i autoGenerate them in AutoGenerateField Event of database

Bill No
Partyname
NoOfGoods
Freight

now freight should be (NoOfGoods*PartyRate from Database)

partyRate should be based on PartyName Entered.

please help

thanks in advance......
Gravatar
9/9/2010 1:43 PM | # re: Silverlight DataForm helpers
fuck u
4/7/2011 12:09 AM | # re: Silverlight DataForm helpers
This guide is deprecated. DataForm.Fields collection has been removed.
silverlight.codeplex.com/wikipage
5/3/2011 9:20 PM | # re: Silverlight DataForm helpers
Is data annotation supported in Telerik SL3 components ( [Requided] [StringLenghth ...] and so on) if class definition is decorated with specific attributes?
Pass4sure 640-760,642-415,Pass4sure 650-575,Pass4sure HP0-S29,Pass4sure HP0-Y30,Pass4sure 000-105,Pass4sure 000-977,Pass4sure COG-112
6/2/2011 9:49 PM | # re: Silverlight DataForm helpers
4/6/2012 7:49 AM | # re: Silverlight DataForm helpers
How do you get to have the ENTER key acts as a TAB key? Anyone, any tip? Globally though, not just the KeyUp/KeyDown event of just a single textbox.

 
Please add 3 and 5 and type the answer here:

DISCLAIMER:

The opinions/content expressed on this blog are provided "ASIS" with no warranties and are my own personal opinions/content (unless otherwise noted) and do not represent my employer's view in any way.