To give you an overview about our problem I start describing our technical starting position:
To take advantage from reusability we try to combine a GridViewDataControl and a RadDataForm within a new custom control (in our example project attached its called "DataFormAlpha"). > see screenshot #1
We introduced new DependencyProperties in the custom control to expose properties from the nested controls and used element to element binding inside XAML to share them with the grid and the dataform. > see screenshot #2
The first problem we encountered was that data annotations are not passed through when the RadDataForm is in mode “AutoGenerateFields = False”. As a workaround we created an own control inherited from DataFormDataField where we read out the display attribute by reflection and set the values to the related Label and Description properties in the OnApplyTemplate event handler. Please also refer to http://www.telerik.com/community/forums/silverlight/data-form/display-annotation.aspx > see screenshot #3
Unfortunately the values are not displayed initially because the event is not thrown if the CurrentItem property of the dataform is bound. > see screenshot #4
Without assigning the property the workaround is working fine but the DataForm CurrentItem is not set according to the GridView item which was selected anymore.
Moreover the custom CurrentItem dependency property does not update the bound object from the viewmodel , although it was set explicitly to “Mode=TwoWay”. > see screenshot #5
Please give us advice if the general approach is feasible and how to proceed with the exposing of dependency properties from user control to parent control, data annotation and CurrentItem problem.
A running example showing the behaviour mentioned above can be found here https://rapidshare.com/files/3847127994/TestDataForm.zip
Screenshots are enclosed.
Thank you for your assistance
Daniel Koffler
9 Answers, 1 is accepted
Unfortunately, currently there is no clean approach to achieving this behavior with manually generated fields. Basically, the whole process of autogenerating DataFormDataFields is a lot different, as they are initially designed according to its source property's declaration. A possible solution is to bind the Label property using a converter. I have modified your project implementing this. However, I would advise you to determine your fields' Label value at a later point of your application's lifecycle, instead of embedding it in your control's core logic.
All the best,
Ivan Ivanov
the Telerik team
thank you very much for your valuable feedback!
Referring to "However, I would advise you to determine your fields' Label value at a later point of your application's lifecycle, instead of embedding it in your control's core logic.": Could you please give advice how you would solve it. What could this "later point" be?
Thanks for your assistance
Daniel Koffler
A possible approach is to determine the respective property on field's loaded event and then read its attributes. However, I presume that you can actually accomplish this task by using autogenerated fields, as you can easily control the autogeneration process by subscribing to RadDataForm's AutoGeneratingField event. In this way you will benefit from autogenerated fields' better DataAnnotations support, being able to customize a lot of features as custom editors, labels etc. Would you please shed some more light on your requirements and especially on your considerations for using manually defined fields, so that I could prepare an example project for you that demonstrates this approach?
All the best,
Ivan Ivanov
the Telerik team
maybe its the force of habit but up to now I always deactived any auto-generation in the GridView and now I do the same in the data form. Main reason for it is that I would like to have full control about the layout and also I do not want to do the layouting by adding some code the code behind because I lose the designer support. But maybe I will change my opinion ...
Attached please find some layouts which we would like to build using the dataform in future. Please let me know it is possible to have the same with auto-generated fields. As already mentioned we use the dataform within a user control to standardize data entry scenarios. To layout the dataform I would have to use the dataform's AutoGeneratingField event. But I need this event at the level of the user control. Do you recommend to expose the event to the user control level?
Thank you for your support.
Daniel
PS: You mentioned the field's loaded event. Do you refer to a dataform event? Would this replace the converter solution?
Please, excuse me for the late reply. I have examined the screenshot that you have provided and I think there won't be any obstacles for you to implement such a solution with autogenerated fields. In order to achieve this you will have to subscribe to RadDataForm's AutogeneratingField event and place your predefined DataTemplate as DataField's content template. However, since I have noticed that there are a numerous TextBoxes and DateTimePickers in your views, I believe that you will have to apply this approach only few times, relying on hte default editors for most of the fields. Mentioning the Loaded event, regarding the topic we discussed in the previous few posts, I have provided it as an alternative point at your application's lifecycle. It can definetly replace the converter approach, but I think that it is not a clear and universal approach itself. Please, let me know whether you would try using the "autogeneration" approach and if so, do not hesitate to ask us to prepare some code samples if such are needed.
Regards,
Ivan Ivanov
the Telerik team
Sorry to bother you again. But I am still struggling a bit finding the best solution for using data annotations within a dataform. Summarizing your answers I see three approaches at the moment.
- Bind label, description properties using a convert:
Problem is that this only works if an item is selected in the data form. If there is no selected item the label, description ... is empty. Right? Do you see any solution for this? - Loading event of the DataFormField:
Please find enclosed an example with this approach. I worry that my way of doing it is not generic enough, could you please review the "MyDataFormDataField_Loaded" code and give feedback?
https://rapidshare.com/files/3028870914/TestDataForm.zip - Subscribe to RadDataForm's AutogeneratingField event:
Could you please provide an example describing this approach. It would be useful to see how this works if you have to put dataform fields on different tabs, add gridviews and have fields (e.g. if they are small to save space) side by side. (see screenshot attached). I would also like to know if it is possible to combine the autogenerating and adding dataform fields manually.
Thank you for your kind assistance
Daniel
Unfortunately, it appears that I have misunderstood your requirements and it won't be possible to place autogenerated data fields in separate tab items. Actually you can combine autogenerated fields and a manually set DataTemplate, but in this case they are placed in a common StackPanel.
Greetings,
Ivan Ivanov
the Telerik team
thanks for clarification. To avoid another misunderstanding. Could you please review the data field's loaded event to gather the data annotions: https://rapidshare.com/files/3028870914/TestDataForm.zip (refering to "A possible approach is to determine the respective property on field's loaded event and then read its attributes" in your answer from 28th October.)
Thanks for your kind assistance
Daniel
I have looked through the code and in my opinion it seems to be fine. However, I have noticed that you have a "todo" comment line that implies you intentions to add additional logic for the Required attribute support. Actually RadDataForm supports all DataAnnotations validation attributes (Required, Range, RegularExpression, CustomValidation etc.) natively, even on custom generated templates. Would you please share with us whether you need to implement any additional functionalities regarding this matter? Please, do not hesitate to contact us if any further inquiries occur.
Regards,
Ivan Ivanov
the Telerik team