Search code examples
c#wpfpropertygridopenfiledialog

C# WPF property grid file browser


I have a property grid connected with public class properties. As I have seen in many solutions by adding an EditorAttribute I should be able to use a file browser:

public class properties
{
    public properties()
    {
        PartProgramConfigurationFilename = "Unknow";
    }

    [Category("File")]
    // BELOW CUSTOM EDITOR
    [EditorAttribute(typeof(System.Windows.Forms.FileDialog), typeof(System.Drawing.Design.UITypeEditor))]
    [Description("Description"), DisplayName("PP configuration filename")]
    public string PartProgramConfigurationFilename { get; set; }
}

So now what I expected is that when I click on the property grid a FileBroswer appears:

enter image description here }

but nothing appears.

I have also followed this solution but again no result.


Solution

  • Unfortunately there is no custom editor out of the box, so I wrote one myself. Here is the code;

    XAML:

    <UserControl x:Class="MyControls.PropertyGridFilePicker"
                 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" 
                 xmlns:local="clr-namespace:Engine.Controls"
                 mc:Ignorable="d" 
                 d:DesignHeight="20" d:DesignWidth="300"
                 x:Name="TheControl">
        <DockPanel>
            <Button x:Name="PickFileButton" Content="…" Click="PickFileButton_Click" DockPanel.Dock="Right" Width="15" />
            <TextBox Text="{Binding ElementName=TheControl, Path=Value}" />
        </DockPanel>
    </UserControl>
    

    Code Behind:

    using Microsoft.Win32;
    using System.Windows;
    using System.Windows.Data;
    using Xceed.Wpf.Toolkit.PropertyGrid;
    using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
    
    namespace MyControls
    {
        /// <summary>
        /// Interaction logic for PropertyGridFilePicker.xaml
        /// </summary>
        public partial class PropertyGridFilePicker : ITypeEditor
        {
            public PropertyGridFilePicker()
            {
                InitializeComponent();
            }
    
            public string Value
            {
                get { return (string)GetValue(ValueProperty); }
                set { SetValue(ValueProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty ValueProperty =
                DependencyProperty.Register("Value", typeof(string), typeof(PropertyGridFilePicker), new PropertyMetadata(null));
    
    
    
            public FrameworkElement ResolveEditor(PropertyItem propertyItem)
            {
                Binding binding = new Binding("Value");
                binding.Source = propertyItem;
                binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
                BindingOperations.SetBinding(this, ValueProperty, binding);
                return this;
            }
    
            private void PickFileButton_Click(object sender, RoutedEventArgs e)
            {
                OpenFileDialog fd = new OpenFileDialog();
                if (fd.ShowDialog() == true && fd.CheckFileExists)
                {
                    Value = fd.FileName;
                }
            }
        }
    }
    

    And this is how you use it:

    public class MySampleClass
    {
        [Editor(typeof(MyControls.PropertyGridFilePicker), typeof(MyControls.PropertyGridFilePicker))]
        public string SomeDataModelString { get; set; }
    }
    

    Credit goes to Brian Lagunas for this tutorial.