C# WPF property grid file browser

醉酒当歌 提交于 2020-08-08 05:18:30

问题


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:

}

but nothing appears.

I have also followed this solution but again no result.


回答1:


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.



来源:https://stackoverflow.com/questions/41677082/c-sharp-wpf-property-grid-file-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!