2017-01-16 2 views
2

パブリッククラスプロパティに接続されたプロパティグリッドがあります。私は、ファイルブラウザを使用することができるはずEditorAttributeを追加することによって、多くのソリューションで見てきたように :C#WPFプロパティグリッドファイルブラウザ

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; } 
} 

だから今、私が期待したものは、私はプロパティグリッドをクリックするとFileBroswerが表示されていることです

enter image description here }

しかし、何も表示されません。

私はまたthis溶液に従っていますが、再び結果はありません。

+1

FileDialogエディターは、winforms propertygridでのみサポートされています。 –

+0

WPFでこれを行うにはどうすればいいですか? – Patrick

+0

それは私が推測するプロパティグリッドに依存します。その領域にはMicrosoftから提供されたものはありません。 –

答えて

1

残念ながら、カスタムエディタは用意されていないので、自分で作成しました。ここにコードがあります。

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> 

コード:

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; 
      } 
     } 
    } 
} 

そして、これはあなたがそれを使用する方法である:

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

クレジットthis tutorialのためにブライアンLagunasに行きます。