DataTemplate in PropertyGrid

Grids for WPF Forum

Posted 13 years ago by Denison Bollay
Version: 10.2.0532
Avatar
When I use the DataTemplate below, it works fine in a ListBox, but not when applied to a PropertyGrid. When I create a PropertyEditor subclass with a ValueTemplateKey, it recognizes the template and displays three lines, but doesn't fill in the values. (I don't want to use ExpandableObjectConverter because in my real code, I will be using a much more complicated DataTemplate).

<Window x:Class="PropertyGridDataTemplateTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:propgrid="http://schemas.actiprosoftware.com/winfx/xaml/propgrid"
xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:PropertyGridDataTemplateTest"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="pvKey" >
<StackPanel>
<TextBox Text="{Binding Pressure}"/>
<TextBox Text="{Binding Volume}" />
<TextBox Text="{Binding Temperature}" />
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type local:PressureVolumeTemperature}">
<StackPanel>
<TextBox Text="{Binding Pressure}" />
<TextBox Text="{Binding Volume}" />
<TextBox Text="{Binding Temperature}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<propgrid:PropertyGrid BorderThickness="0" Name="PropGrid1"/>
<ListBox Height="100" Name="listBox1" Width="120" />
</StackPanel>
</Grid>
</Window>

namespace PropertyGridDataTemplateTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
PropGrid1.SelectedObject = new Test() { PV = new PressureVolumeTemperature() { Pressure = 1, Volume = 2, Temperature = 3 } };
List<PressureVolumeTemperature> myList = new List<PressureVolumeTemperature>();
for (int i = 0; i < 2; i++)
myList.Add(new PressureVolumeTemperature() { Pressure = 1, Volume = 2, Temperature = 3 });
listBox1.ItemsSource = myList;
}
}

public class Test
{

[Editor(typeof(PressureVolumePropertyEditor), typeof(PropertyEditor))]
public PressureVolumeTemperature PV { get; set; }

}

public class PressureVolumeTemperature
{
public double Pressure { get; set; }
public double Volume { get; set; }
public double Temperature { get; set; }

}

public class PressureVolumePropertyEditor : PropertyEditor
{
public PressureVolumePropertyEditor()
: base()
{
PropertyType = typeof(PressureVolumeTemperature);
}

/// <summary>
/// Gets or sets the resource key that references a System.Windows.DataTemplate for the value cell.
/// </summary>
public override object ValueTemplateKey
{
get { return "pvKey"; }
}
}


}

Comments (2)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Denison,

Please see the DataTemplates defined in the Property Editors QuickStart. Unlike the ListBox, the DataContext of the DataTemplate's root element is not set to the "item" you are passing in. You need use a RelativeSource that looks for a IPropertyDataAccessor.

Normally, each property will be displayed separately and you'd access it's value using the IPropertyDataAccessor.Value property. But you could access sub-properties using Value.Pressure in your Binding statements. So something like the following should work:

{Binding Value.Pressure, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}}

You may want to take a look at the category editors also, as those allow you to present one or more properties using a single DataTemplate, like you are trying to do above, and prevent them from otherwise showing up in the PropertyGrid.


Actipro Software Support

Posted 13 years ago by Denison Bollay
Avatar
Thats works. I had tried almost every other conceivable variation.
Thanks!
The latest build of this product (v24.1.2) was released 0 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.