gpt4 book ai didi

c# - PropertyGrid 能否为不同的选定对象值自定义显示,而不是空白?

转载 作者:行者123 更新时间:2023-11-30 16:19:01 26 4
gpt4 key购买 nike

当所选对象具有不同的属性值时,是否可以自定义属性的显示值?

网格的默认行为是当所有选定的对象具有相同的值时显示一个值,但当它们不同时简单地空白该字段。无法知道它们有何不同。

例如给定以下类和代码,是否可以将检查器和类配置为显示如下内容(整数值的范围,其他任何值的倍数)

TestLong|[50 - 60]
TestInt|10
TestEnum|[Multiple]

也就是说,如果值不同,则显示一些表明它们有何不同的东西,但如果它们都相同,则显示该值?

public enum TestEnum
{
EnumVal1,
EnumVal2,
EnumVal3
}

public class TestClass
{
public long TestLong { get; set; }
public int TestInt { get; set; }
public TestEnum TestEnum { get; set; }
}

...
control.SelectedObjects = new []
{
new TestClass
{
TestLong = 50,
TestInt = 10,
TestEnum = TestEnum.EnumVal1
},
new TestClass
{
TestLong = 60,
TestInt = 10,
TestEnum = TestEnum.EnumVal3
},
}
...

最佳答案

我认为您不能更改显示,因为 PropertyGrid 一般使用 TypeConverters (包括隐含的)显示值,但对于多选,不使用。

虽然您可以做的是提出一个自定义的 UITypeEditor,但这并不是正确的答案。当网格处于多选模式时,像这样:

    public class TestClass
{
// decorate the property with a custom UITypeEditor
[Editor(typeof(MyMultiSelectionEditor), typeof(UITypeEditor))]
public long TestLong { get; set; }
public int TestInt { get; set; }
public TestEnum TestEnum { get; set; }
}

public class MyMultiSelectionEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
// adapt to your need
if (!IsPropertyGridInMultiView(context))
return UITypeEditorEditStyle.None;

return UITypeEditorEditStyle.Modal;
}

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (IsPropertyGridInMultiView(context))
{
// multi view, show my custom stuff
MessageBox.Show("hello from multi selection");
}
return base.EditValue(context, provider, value);
}

// gets a PropertyGrid instance from the context, if any
private static PropertyGrid GetPropertyGrid(ITypeDescriptorContext context)
{
IServiceProvider sp = context as IServiceProvider;
if (sp == null)
return null;

Control view = sp.GetService(typeof(IWindowsFormsEditorService)) as Control;
if (view == null)
return null;

return view.Parent as PropertyGrid;
}

// determines if there is a PropertyGrid in the context, and if it's selection is multiple
private static bool IsPropertyGridInMultiView(ITypeDescriptorContext context)
{
PropertyGrid pg = GetPropertyGrid(context);
if (pg == null)
return false;

return pg.SelectedObjects != null && pg.SelectedObjects.Length > 1;
}
}

关于c# - PropertyGrid 能否为不同的选定对象值自定义显示,而不是空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15560875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com