
Background:
I'm adding a PropertyEditor to my PropertyGrid that uses a custom ValueTemplate. The custom DataTemplate contains a ComboBox, which works as desired. However, my custom DataTemplate's ComboBox doesn't match the appearance of a built-in PropertyGrid ComboBox.
Question:
How can I make my custom template's ComboBox have the same appearance as the PropertyGrid's built-in ComboBox?
Sample Code:
MainWindow.xaml
<Window
x:Class="PropertyGridCustomTemplates.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:Collections="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:propertyGridCustomTemplates="clr-namespace:PropertyGridCustomTemplates"
Title="MainWindow"
Height="350"
Width="525"
>
<Window.Resources>
<!--
This ComboBox will be empty; I'm just using this to force a custom ComboBox to appear in the PropertyGrid.
-->
<DataTemplate
x:Key="bobTemplate"
>
<ComboBox
DisplayMemberPath="Name"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}, Path=DataContext}"
SelectedValue="{Binding RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}, Path=DataContext, ValidatesOnDataErrors=True, Mode=TwoWay}"
SelectedValuePath="Bob"
/>
</DataTemplate>
<Collections:ArrayList
x:Key="propertyGridEditorsList"
>
<propgrid:PropertyEditor
ObjectType="{x:Type propertyGridCustomTemplates:SampleViewModel}"
PropertyName="Bob"
ValueTemplate="{StaticResource bobTemplate}"
/>
</Collections:ArrayList>
<propertyGridCustomTemplates:SampleViewModel x:Key="bob"/>
</Window.Resources>
<Grid>
<propgrid:PropertyGrid
x:Name="propertyGrid"
SelectedObject="{StaticResource bob}"
/>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Collections;
using System.Linq;
using System.Windows;
using ActiproSoftware.Windows.Controls.PropertyGrid.Editors;
namespace PropertyGridCustomTemplates
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var propEditors = this.Resources["propertyGridEditorsList"] as ArrayList;
propertyGrid.PropertyEditors.Clear();
propertyGrid.PropertyEditors.AddRange(propEditors.Cast<PropertyEditor>());
}
}
}
SampleViewModel.cs:
using System.Windows;
namespace PropertyGridCustomTemplates
{
public class SampleViewModel
{
public string Bob { get; set; }
public HorizontalAlignment Alignment { get; set; }
}
}
Thanks,
-Craig