How can I make a PropertyEditor's ValueTemplate's control appear like built-in controls?

Grids for WPF Forum

Posted 11 years ago by Craig - Varigence, Inc.
Version: 12.2.0573
Avatar

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

Comments (1)

Answer - Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

I believe you have WPF Studio and thus can download the default styles and templates for all the controls. If you open up the PropertyEditors.xaml file, search for ComboBox and you can see the various settings we use. Then clone those things for your own template.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.