
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"; }
}
}
}
<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"; }
}
}
}