You can accomplish this using
CellTemplate
(the property name should be similar) for the Column.
Write a
DataTemplate
with DataTriggers. These
DataTrigger
would help in showing and hiding the different controls.
Lets go to your problem:
Did you checked whether the ValueChanged handler executes all the times (it should be equal to the number of items in the list)?
Thing I noticed:
Even the data is not DateTime it would show the control. It should be like below
if (eventArgs.NewValue != null && eventArgs.NewValue is DateTime)
{
picker.SelectedDate = (DateTime)eventArgs.NewValue;
((UniversalControl)sender).Content = picker;
((UniversalControl)sender).Visibility = Visibility.Visible;
}
The below code might throw runtime error if the
eventArgs.NewValue
is
null
. Avoid using
&&
operator while checking for
!= null
if (eventArgs.NewValue != null && eventArgs.NewValue is DateTime)
Suggestion:
Instead of casting
UniversalControl
everytime you can define a variable at the beginning of the method and start using it.
Update:
UniversalControl style doesn't have any ContentPresenter to show the Content you add dynamically from the codebehind.
It should be like below:
<Style TargetType="local:UniversalControl">
<setter property="Template">
<setter.value>
<controltemplate targettype="local:UniversalControl">
<border removed="{TemplateBinding Background}">
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<contentpresenter />
</border>
</controltemplate>
</setter.value>
</setter>
</Style>
See, I have added a ContentPresenter to show the Content. :)
Mark it as Answer if it is helpful.