I’ve got a property which is a database data type (char, DateTime, int, float etc…) and I want to change the control used to enter a value of the selected type. So for text values I want a TextBox and for date values I want a DatePicker.
One way I thought about doing it was to have one of each control on my form and set their Visibility using an appropriate IValueConverter implementation. I know this will work, but it would create a lot of code and doesn’t feel very nice.
The other way I thought was to use a ContentPresenter and set its content with a Style and DataTriggers but I can’t get it to work.
<Style x:Key="TypedValueHelper" TargetType="{x:Type ContentPresenter}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=DataType}" Value="Char">
<Setter Property="Content" Value="???"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=DataType}" Value="Date">
<Setter Property="Content" Value="???"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=DataType}" Value="Integer">
<Setter Property="Content" Value="???"/>
</DataTrigger>
</Style.Triggers>
</Style>
If anyone can fill in my “???” or offer a better solution please do.
You could do a combination of style with setters and DataTemplates. You basically have the start for it in your code, although I don’t think
ContentPresenteris the right control to style, since it does not have a template.Create a style like this:
Then use the style in the
ContentControl: