Is it possible to specify the display format for a property in the PropertyGrid?
I've tried the [DisplayFormat], but neither DataFormatString nor NullDisplayText seems to be making any difference whatsoever.
In my particular case, I want the DateTime property to be displayed as 'yyyy - MM - dd', but (as I mentioned), decorating the property with [DisplayFormat(DataFormatString = "yyyy - MM - dd")] doesn't work.
As far as I have noticed, PropertyGrid selects RadDateTimePicker for DateTime types by default, but for some dates I'd like the 'date' part only. I don't care much about editing (although that would be nice too), but it would be great to adjust the display for read-only property grids.
Is there any way to make that work?
(Controls version used: 2013.2.724)
5 Answers, 1 is accepted
The thing is that string format of the binding should be set differently in RadDateTimePicker (the default editing element for DateTime properties). Check out this article for a reference. What you can try is to define the property definition like:
<telerik:RadPropertyGrid.PropertyDefinitions> <telerik:PropertyDefinition Binding="{Binding Established, StringFormat=\{0:MM/dd/yyyy\}}" IsReadOnly="True"> <telerik:PropertyDefinition.EditorTemplate> <DataTemplate> <telerik:RadDatePicker SelectedDate="{Binding Established}" IsReadOnly="{Binding IsReadOnly}"/> </DataTemplate> </telerik:PropertyDefinition.EditorTemplate> </telerik:PropertyDefinition> </telerik:RadPropertyGrid.PropertyDefinitions>Is that approach suitable for your sceanrio ?
Regards,
Maya
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Your link pointed me in the right direction though, thanks!
I ended up hooking to OnFieldLoaded of the grid and checking the content. Then I get to the 'Binding.StringFormat' and set the DateTimeFormat in the CultureInfo of the date picker. It's a hack, but it works...
Hi,
is there a way to provide this format definition as Data Annotation? [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")] does not work ...
Regards
Achieved the expected behaviour with [Telerik.Windows.Controls.Data.PropertyGrid.Editor(typeof(RadDatePicker), "SelectedValue")]
:-)
This might be done by subscribing to the RadPropertyGrid's AutoGeneratingPropertyDefinition event in the constructor of ones UserControl like so:
PropertyGrid1.AutoGeneratingPropertyDefinition += PropertyGrid1_AutoGeneratingPropertyDefinition;
private void PropertyGrid1_AutoGeneratingPropertyDefinition(object sender,
Telerik.Windows.Controls.Data.PropertyGrid.AutoGeneratingPropertyDefinitionEventArgs e)
{
if (e.PropertyDefinition.Binding is Binding binding && e.PropertyDefinition.SourceProperty.PropertyType == typeof(DateTime))
{
binding.StringFormat = "'yyyy - MM - dd";
}
}
N.B also works for other types (e. g double)