DateTime Editor in WPF property grid

Grids for WPF Forum

Posted 4 years ago by Bruce Lum
Version: 16.1.0630
Platform: .NET 4.0
Environment: Windows 7 (64-bit)

I'm trying to dynamically add a date property to a WPF property grid at run-time, since it is retrieved from a database. I can get the date property to properly appear via XAML code:

<propgrid:PropertyGrid Name="propertyGrid1"
    Width="508" IsSummaryVisible="False">
    <propgrid:PropertyGridCategoryItem DisplayName="DateTime">
          DisplayName="DateTimeEditBox (date only)"
          Description="A DateTimeEditBox used to modify the date of a DateTime.">
          <system:DateTime>10/31/2008 12:00:00</system:DateTime>
          <system:DateTime>1/1/2008 12:00:00</system:DateTime>
                Margin="0" BorderThickness="0"
                Value="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}, Mode=TwoWay}"
                Format="d" />

However, I have a hard time converting this into equivalent C# code. The following C# code does not work. If I comment out the Value property assignment for the DateTimeEditBox object, only a blank value cell is displayed.

propertyGrid1.IsSummaryVisible = false;

DateTimeEditBox dateTimeEditor = new DateTimeEditBox();
dateTimeEditor.Margin = new Thickness(0);
dateTimeEditor.BorderThickness = new Thickness(0);
dateTimeEditor.Format = "d";

// This line emits a compile error
dateTimeEditor.Value =
    "{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}, Mode=TwoWay}";   

PropertyGridPropertyItem item = new PropertyGridPropertyItem();
item.DisplayName = "DateTimeEditBox (date only)";
item.Description = "A DateTimeEditBox used to modify the date of a DateTime.";
item.ValueTemplate = new DataTemplate(dateTimeEditor);
item.ValueType = typeof(DateTime);
item.DefaultValue = DateTime.Now;
item.Value = DateTime.Now;

Any help would be greatly appreciated.

Bruce Lum
Contact Innovations Inc.

Comments (3)

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Bruce,

It's difficult to create a DataTemplate properly in code.  I'd recommend that you make your DateTimeEditBox-based DataTemplate in your app's Resouces and then use a FindResource() call to get it and assign it to item.ValueTemplate.  That way, you can skip the whole DateTimeEditBox configuration part in C#.

On a side note, the Value line you have there is invalid as VS reports.  You'd need to create a Binding programmatically and then call SetBinding to apply it on the target control.

Actipro Software Support

Posted 4 years ago by Bruce Lum

Thanks for your reply.

I wouldn't know how to make a DataTemplate in my application's Resources, let alone make any Resources, because of my lack of knowledge to do so.

However, I found that the following code is working so far, but haven't fully tested it. The code is derived from that of another post.

FrameworkElementFactory factory = new FrameworkElementFactory(typeof(DateTimeEditBox));
factory.SetValue(DateTimeEditBox.MarginProperty, new Thickness(0));
factory.SetValue(DateTimeEditBox.IsReadOnlyProperty, false);
factory.SetValue(DateTimeEditBox.BorderThicknessProperty, new Thickness(0));
factory.SetValue(DateTimeEditBox.FormatProperty, "MMM dd yyyy");

RelativeSource relativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(IPropertyDataAccessor), 1);
factory.SetBinding(DateTimeEditBox.ValueProperty, new Binding("Value") { RelativeSource = relativeSource, Mode = BindingMode.TwoWay });   

PropertyEditor propertyEditor = new PropertyEditor() { PropertyType = typeof(DateTime), ValueTemplate = new DataTemplate() { VisualTree = factory } };

PropertyGridPropertyItem item = new PropertyGridPropertyItem();
item.Tag = 3;
item.DisplayName = "DateTime";
item.Description = "DateTime object";
item.ValueType = typeof(DateTime);
item.DefaultValue = DateTime.Now;
item.Value = DateTime.Now;

I don't quite fully understand the code that creates the property editor. It does display a Date/Time editor for just the date (in the month day, year format).

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Bruce,

For a DataTemplate, you just copy the DataTemplate you posted in XAML, add an x:Key="SomeKeyValue" attribute to it and put it your Application.Resources.  Then doing a 'this.FindResource("SomeKeyValue") as DataTemplate' call should be able to locate and return it and prevent the whole element factory section of logic you are doing.  There should be a lot of help on the web for putting DataTemplates in Application.Resources and how all that works.

[Modified 4 years ago]

Actipro Software Support

The latest build of this product (v2019.1 build 0685) was released 4 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.