C# WPF 属性 网格文件浏览器
C# WPF property grid file browser
我有一个 属性 网格与 public class 属性相连。
正如我在许多解决方案中看到的那样,通过添加 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; }
}
所以现在我期望的是,当我单击 属性 网格时,会出现一个 FileBroswer:
}
但什么也没有出现。
我也遵循了this解决方案,但还是没有结果。
不幸的是没有现成的自定义编辑器,所以我自己写了一个。这是代码;
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; }
}
感谢 Brian Lagunas 的 this tutorial。
我有一个 属性 网格与 public class 属性相连。 正如我在许多解决方案中看到的那样,通过添加 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; }
}
所以现在我期望的是,当我单击 属性 网格时,会出现一个 FileBroswer:
但什么也没有出现。
我也遵循了this解决方案,但还是没有结果。
不幸的是没有现成的自定义编辑器,所以我自己写了一个。这是代码;
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; }
}
感谢 Brian Lagunas 的 this tutorial。