简体   繁体   English

C# WPF 属性网格文件浏览器

[英]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:正如我通过添加 EditorAttribute 在许多解决方案中看到的那样,我应该能够使用文件浏览器:

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:所以现在我期望的是,当我单击属性网格时,会出现一个 FileBroswer:

在此处输入图片说明 } }

but nothing appears.但什么也没有出现。

I have also followed this solution but again no result.我也遵循了这个解决方案,但同样没有结果。

Unfortunately there is no custom editor out of the box, so I wrote one myself.不幸的是,没有现成的自定义编辑器,所以我自己写了一个。 Here is the code;这是代码;

XAML: 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 . 本教程归功于 Brian Lagunas。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM