This is a migrated thread and some comments may be shown as answers.

DataForm Data Template - UI Refresh

1 Answer 128 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 08 Feb 2012, 02:51 AM
When changing the templates of a RadDataForm the UI will not be updated with the new template until the CurrentItem changes. This is because the dependency properties for the templates do not trigger any events. The changing of a datatemplate should trigger an event, and in the event handler it should force a refresh to the UI.

I've extended the RadDataForm so that it supports DataTemplateSelectors, the template is then selected based on the CurrentItem when the CurrentItemChanged event is triggered.

So for instance if I change the CurrentItem the following sequence of events occur.
1. CurrentItem is set
2. Check auto edit,  triggers UI refresh etc (Base class RadDataForm)
3. invokes the CurrentItemChanged event (Base class RadDataForm)
4. CurrentItemChanged event is handled (My inherrited class)
5. Template is assigned (My inherrited class)

The problem in this sequence of events is that the UI gets refreshed before the correct data template is applied.

As a workaround I decided to wrap the CurrentItem object, with a new property called 'SelectedItem'. This is obviously a bit of a hack but it does the trick.

public class RadDataFormEx : RadDataForm
   {
       public object SelectedItem
       {
           get
           {
               return (object)GetValue(SelectedItemProperty);
           }
           set
           {
               this.SetValue(SelectedItemProperty, value);
           }
       }
 
 
       public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof(object), typeof(RadDataFormEx), new PropertyMetadata(null, RadDataFormEx_SelectedItem_PropertyChangedCallback));
 
       private static void RadDataFormEx_SelectedItem_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
       {
           if(d is RadDataFormEx)
           {
               RadDataFormEx form = d as RadDataFormEx;
               form.SelectTemplate(e.NewValue);
 
               if (form.CurrentItem != e.NewValue)
               {
                   form.CurrentItem = e.NewValue;
               }
                
           }
       }
 
 
       private DataTemplateSelector m_CurrentReadOnlyItemTemplateSelector;
 
       public DataTemplateSelector CurrentReadOnlyItemTemplateSelector
       {
           get { return m_CurrentReadOnlyItemTemplateSelector; }
           set { m_CurrentReadOnlyItemTemplateSelector = value; }
       }
 
       private DataTemplateSelector m_CurrentEditItemTemplateSelector;
 
       public DataTemplateSelector CurrentEditItemTemplateSelector
       {
           get { return m_CurrentEditItemTemplateSelector; }
           set { m_CurrentEditItemTemplateSelector = value; }
       }
 
       private DataTemplateSelector m_CurrentNewItemTemplateSelector;
 
       public DataTemplateSelector CurrentNewItemTemplateSelector
       {
           get { return m_CurrentNewItemTemplateSelector; }
           set { m_CurrentNewItemTemplateSelector = value; }
       }
 
       public RadDataFormEx()
       {
           this.CurrentItemChanged += RadDataFormEx_CurrentItemChanged;
       }
 
       void RadDataFormEx_CurrentItemChanged(object sender, EventArgs e)
       {
           if (SelectedItem != CurrentItem)
           {
               SelectedItem = CurrentItem;
           }
       }
 
       protected void SelectTemplate(object newItem)
       {
     
           if (CurrentReadOnlyItemTemplateSelector != null)
               this.ReadOnlyTemplate = CurrentReadOnlyItemTemplateSelector.SelectTemplate(newItem, this);
           else
               this.ReadOnlyTemplate = null;
 
           if (CurrentNewItemTemplateSelector != null)
               this.NewItemTemplate = CurrentNewItemTemplateSelector.SelectTemplate(newItem, this);
           else
               this.NewItemTemplate = null;
 
           if (CurrentEditItemTemplateSelector != null)
               this.EditTemplate = CurrentEditItemTemplateSelector.SelectTemplate(newItem, this);
           else
               this.EditTemplate = null;
 
       }
 
 
   }


In this example I can bind to the 'SelectedItem', then apply the data template, and then assign the value to 'CurrentItem'. This is similar to some source code I posted in another thread, but this code addresses the issue of the UI refresh.

I would like to see four things in next release
A) DataTemplateSelector support
B) Automatic refresh of UI when data templates change.
C) More protected functions instead of private (On all controls)
     - This would let me force a refresh myself inside an inherrited class
D) More overrideable functions (On all controls)
     - I would rather inherrit/override than to recompile the telerik class libraries





1 Answer, 1 is accepted

Sort by
0
Accepted
Ivan Ivanov
Telerik team
answered on 08 Feb 2012, 03:48 PM
Hi Paul,

Thank you for reporting this issue to us. Unfortunately we won't manage to include the fix in our next official release (Q1 2012), as it is up to be released in several days. However, we will do our best to introduce it with some of the first weekly builds after Q1. As for the data template selectors support, we have considered it as a high priority feature, so that we will surely implement it in the close future. Thank you for your valuable feedback!

Regards,
Ivan Ivanov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
DataForm
Asked by
Paul
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Share this question
or