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

Specify the display format for the property

5 Answers 364 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
TechInsights
Top achievements
Rank 1
TechInsights asked on 08 Aug 2013, 10:46 AM
Hello everyone,

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

Sort by
0
Accepted
Maya
Telerik team
answered on 09 Aug 2013, 08:31 AM
Hello,

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
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
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 >>
0
TechInsights
Top achievements
Rank 1
answered on 27 Aug 2013, 07:54 AM
I didn't want to specify separate XAML views for property grids, but to re-use one auto-generated property grid view I have.

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...
0
Roman
Top achievements
Rank 2
answered on 05 Nov 2018, 06:50 AM

Hi,

is there a way to provide this format definition as Data Annotation? [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")] does not work ...

Regards

0
Roman
Top achievements
Rank 2
answered on 05 Nov 2018, 12:52 PM

Achieved the expected behaviour with [Telerik.Windows.Controls.Data.PropertyGrid.Editor(typeof(RadDatePicker), "SelectedValue")]

 

:-)

0
declan
Top achievements
Rank 1
answered on 14 Apr 2020, 02:58 PM

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)

Tags
PropertyGrid
Asked by
TechInsights
Top achievements
Rank 1
Answers by
Maya
Telerik team
TechInsights
Top achievements
Rank 1
Roman
Top achievements
Rank 2
declan
Top achievements
Rank 1
Share this question
or