This question is locked. New answers and comments are not allowed.
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.
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
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