在对this question的回答中,我建议使用依赖注入,以便为继承自ITypeEditor的类型编辑器提供自定义(通过“向类型编辑器的构造函数传递某些内容”)。
因为这个类型编辑器只是用来修饰适当的参数,所以我不知道该如何做。我没有显式实例化它,所以我不调用它的构造函数。
我在网上找不到解决方案/解释,尽管我找到了一条与类似情况有关的语句(尽管使用的是UITypeEditor),在这种情况下,it was said“由于您没有创建自己的UITypeEditor派生程序的实例,所以无法控制传递给构造函数的参数”。这和我的想法是一致的。
那么,我是否可以访问构造函数来向其传递参数,如果是的话,如何访问呢?
[Editor(typeof(MyControls.PropertyGridFilePicker), typeof(MyControls.PropertyGridFilePicker))]
public string ProjectFolder { get; set; } = "";public partial class PropertyGridFilePicker : ITypeEditor
{
public PropertyGridFilePicker()
{
InitializeComponent();
// Constructor - if I could pass parameters into this it would of course be very useful
}
// The rest of the type editor methods go here
}发布于 2020-08-15 15:25:13
在您的上下文中,依赖注入不能解决问题。正如您已经注意到的,您无法控制类型的实例创建。为了允许依赖注入,API必须支持它,例如,接受抽象工厂作为参数。
为了解决您的问题,您应该创建一个抽象基类型,它封装了所有编辑器的常见行为。与添加配置参数控制/扩展行为相比,使用专门化也更易于维护。给定您的posted code,我假设您希望在按下“浏览”按钮时执行专门的操作。应该将此行为委托给子类,方法是在基类型中将此行为实现为抽象。然后,只需使用特殊类的类型作为EditorAttribute的参数
PropertyGridBrowser.cs
// The abstract common base class. Override 'ExecuteBrowse' to extend behavior.
public abstract class PropertyGridBrowser : UserControl, ITypeEditor
{
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));
protected PropertyGridBrowser()
{
InitializeComponent();
}
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;
}
protected abstract void ExecuteBrowse();
private void PickFileButton_Click(object sender, RoutedEventArgs e)
{
ExecuteBrowse();
}
}PropertyGridFilePicker.cs
public partial class PropertyGridFilePicker : PropertyGridBrowser
{
string rtn = "";
public PropertyGridFilePicker() : base()
{
}
protected override void ExecuteBrowse()
{
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == true && fd.CheckFileExists)
{
Value = fd.FileName;
}
}
}PropertyGridFolderPicker.cs
public partial class PropertyGridFolderPicker : PropertyGridBrowser
{
public PropertyGridFilePicker() : base()
{
}
protected override void ExecuteBrowse()
{
var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
}
}
}示例
// Use a file browser
[Editor(typeof(MyControls.PropertyGridFilePicker), typeof(MyControls.PropertyGridFilePicker))]
public string ProjectFolder { get; set; } = "";
// Use a folder browser
[Editor(typeof(MyControls.PropertyGridFolderPicker), typeof(MyControls.PropertyGridFolderPicker))]
public string ProjectFolder { get; set; } = "";https://stackoverflow.com/questions/63426819
复制相似问题