如何在PropertyGrid中覆盖DockStyle Editor的行为 [英] How can I override behavior of DockStyle Editor in PropertyGrid
问题描述
我们有一个自定义面板,该面板继承了Panel并具有与SplitContainer Orientation属性类似的实现的Orientation属性.对于我们的自定义面板,DockStyle.Fill永远无效,根据Orientation属性值,DockStyle的垂直"需要为左"或右",而水平"则需要为"DockStyle.Fill".
We have a custom panel which inherits Panel and has an Orientation property implemented similarly to the SplitContainer Orientation property. For our custom panel, DockStyle.Fill is never valid and depending on the Orientation property value, DockStyle needs to be Left or Right for Vertical or Top or Bottom for Horizontal.
DockStyleEditor类是密封的,因此我们不能为我们自己的自定义UITypeEditor子类化.有没有一种方法可以使用TypeDescriptor或某些其他方法来覆盖某些行为?
The DockStyleEditor class is sealed so we can't subclass it for our own custom UITypeEditor. Is there a way to override certain behaviors using a TypeDescriptor or some other way?
我们希望对属性网格"中的自定义面板DockStyle编辑器执行以下操作:
1.禁用中间的填充"按钮或将其显示为带有红色斜线的圆圈,以表明该按钮作为选项不可用
2.当方向"属性为水平时,禁用顶部"和底部"按钮
3.当方向"属性为垂直时,禁用向左"和向右"按钮
What we would like to do for our custom panel DockStyle editor in the Property Grid is:
1. Either disable the center Fill button or display it with the red slash circle to indicate it is unavailable as an option
2. Disable the Top and Bottom buttons when the Orientation property is horizontal
3. Disable the Left and Right buttons when the Orientation property is vertical
推荐答案
UITypeEditor
中,您可以创建DockEditor
并使用反射操作其编辑器控件,然后使用它执行值编辑.
DockEditor
is sealed but you can create your custom dock editor based on DockEditor
without inheriting it. In your custom UITypeEditor
, you can create an instance of DockEditor
and using reflection manipulate its editor control and then perform value editing using it.
DockEditor
使用作为私有类的DockUI
控件.它具有用于None
按钮和容器控件的复选框,其中包含用于Fill
,Top
,Left
,Right
和Bottom
的复选框.
然后,您可以根据自己的逻辑简单地更改Enabled
,BackColor
或其他属性.
The DockEditor
uses a DockUI
control which is a private class. It has a check box for None
button and container control which contains check boxes for Fill
, Top
, Left
, Right
and Bottom
.
Then you can simply change Enabled
, BackColor
or other properties based on your logic.
在下面的代码中,我将找到那些复选框按钮,并将禁用Fill
,Top
,Bottom
和None
.用户可用的唯一选项是Left
和Right
:
In the below code I'll find those check box buttons and I will disable Fill
, Top
, Bottom
and None
. The only options available for the user will be Left
and Right
:
public class MyDockEditor : UITypeEditor
{
DockEditor editor;
public MyDockEditor()
{
editor = new System.Windows.Forms.Design.DockEditor();
}
public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
Type dockUiType = typeof(DockEditor)
.GetNestedType("DockUI", BindingFlags.NonPublic);
var dockUiConstructor = dockUiType.GetConstructors()[0];
var dockUiField = typeof(DockEditor)
.GetField("dockUI", BindingFlags.Instance | BindingFlags.NonPublic);
var dockUiObject = dockUiConstructor.Invoke(new[] { editor }) as Control;
dockUiField.SetValue(editor, dockUiObject);
var container = dockUiObject.Controls[0];
var none = dockUiObject.Controls[1];
var fill= container.Controls[0];
var left= container.Controls[1];
var right= container.Controls[2];
var top = container.Controls[3];
var bottom = container.Controls[4];
none.Enabled = false;
fill.Enabled = false;
top.Enabled = false;
bottom.Enabled = false;
return editor.EditValue(context, provider, value);
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return editor.GetEditStyle(context);
}
}
要使用它,只需用Editor属性装饰控件的Dock
属性即可:
To use it it's enough to decorate Dock
property of your control with an Editor attribute:
[Editor(typeof(MyDockEditor), typeof(UITypeEditor))]
public override DockStyle Dock
{
get { return base.Dock; }
set { base.Dock = value; }
}
如您在下图中所看到的,只启用了左侧和右侧.
As you see in below image, only left and right enabled.
这篇关于如何在PropertyGrid中覆盖DockStyle Editor的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!