
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"
Height="217"
HorizontalAlignment="Left"
Margin="47,36,0,0"
VerticalAlignment="Top"
Width="508" IsSummaryVisible="False">
<propgrid:PropertyGrid.Items>
<propgrid:PropertyGridCategoryItem DisplayName="DateTime">
<propgrid:PropertyGridPropertyItem
DisplayName="DateTimeEditBox (date only)"
Description="A DateTimeEditBox used to modify the date of a DateTime.">
<propgrid:PropertyGridPropertyItem.Value>
<system:DateTime>10/31/2008 12:00:00</system:DateTime>
</propgrid:PropertyGridPropertyItem.Value>
<propgrid:PropertyGridPropertyItem.DefaultValue>
<system:DateTime>1/1/2008 12:00:00</system:DateTime>
</propgrid:PropertyGridPropertyItem.DefaultValue>
<propgrid:PropertyGridPropertyItem.ValueTemplate>
<DataTemplate>
<editors:DateTimeEditBox
Margin="0" BorderThickness="0"
Value="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}, Mode=TwoWay}"
Format="d" />
</DataTemplate>
</propgrid:PropertyGridPropertyItem.ValueTemplate>
</propgrid:PropertyGridPropertyItem>
</propgrid:PropertyGridCategoryItem>
</propgrid:PropertyGrid.Items>
</propgrid:PropertyGrid>
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;
propertyGrid1.Items.Clear();
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;
propertyGrid1.Items.Add(item);
Any help would be greatly appreciated.
Bruce Lum
Developer
Contact Innovations Inc.