经过一番搜索和阅读http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid&referringTitle=Documentation
我认为在我的情况下至少有两种方式。
1。自定义 xctk:CollectionControl 并编辑 XAML。
<xctk:PropertyGrid Name="_generalPropertyGrid" DockPanel.Dock="Top"
ShowSearchBox="False" ShowSortOptions="False" ShowTitle="False" NameColumnWidth="120" BorderThickness="0">
<xctk:PropertyGrid.EditorDefinitions>
<xctk:EditorTemplateDefinition TargetProperties="CarsCollection">
<xctk:EditorTemplateDefinition.EditingTemplate>
<DataTemplate>
<xctk:CollectionControl SelectedItem="{Binding Path=SelectedCar}"/>
</DataTemplate>
</xctk:EditorTemplateDefinition.EditingTemplate>
</xctk:EditorTemplateDefinition>
</xctk:PropertyGrid.EditorDefinitions>
</xctk:PropertyGrid>
2。创建自己的UserControl,实现ITypeEditor
查看http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid&referringTitle=Documentation 中的数据,由ComboBox 选择。我选择这种方式。
<UserControl x:Class="proj_namespace.CarSelector"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Name="CarSelector"
d:DesignHeight="25" d:DesignWidth="200">
<Grid>
<ComboBox ItemsSource="{Binding Value, ElementName=CarSelector}" SelectionChanged="Selector_OnSelectionChanged"/>
</Grid>
</UserControl>
以及后面的类代码:
public partial class CarSelector : ITypeEditor
{
public CarSelector()
{
InitializeComponent();
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(ObservableCollection<string>), typeof(CarSelector),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
var binding = new Binding("Value");
binding.Source = propertyItem;
binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
BindingOperations.SetBinding(this, ValueProperty, binding);
return this;
}
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender != null)
MainWindow.Instance._properties.SelectedCar = (sender as ComboBox).SelectedItem as string;
}
}
最后在属性上方添加自定义编辑器行
[Editor(typeof(CarSelector), typeof(CarSelector))]
public ObservableCollection<string> CarsCollection { get { return _securities; } }