c# - 如何格式化/更改属性网格中的显示值

标签 c# .net winforms propertygrid typeconverter

我正在尝试使用 Winform Property Grid,但我无法格式化显示的值(头脑与 wpf 联系太紧密)现在)

所以我想要的是,属性网格中有一个下拉菜单,它有自己的UITypeEditor,该编辑器显示诸如

之类的值
1 - On
2 - Off
3 - Unknown

所以监听 propertyGrid 变化的属性是 int ,由于一些奇怪的原因,我无法将其更改为字符串,所以就像在 wpf 中一样,我可以有一个转换器之类的东西,将 1 转换为 1- On1-On 到 1

如何将我的属性(property)或属性(property)网格装饰得如此智能?

我的属性(property)看起来像

[LocalizedCategory("Limits", typeof(Api.Properties.Resources))]
[LocalizedDisplayName("Maximum", typeof(Api.Properties.Resources))]
[LocalizedDescription("Maximum", typeof(Api.Properties.Resources))]
[Editor(typeof(TextConversionTypeEditor), typeof(UITypeEditor))]
public int CriticalMaximum
{
    get; set;
}

我可以让我的属性网格显示更多信息而不仅仅是一个 int 吗?

最佳答案

如果您可以使用 Enum 作为属性的类型,那么它会在下拉列表中显示可用值,否则您可以创建一个 TypeConverter 来提供 drop 的值-向下。为此,您可以使用以下任一选项:

将枚​​举的 TypeConverter 用于 int 属性

如果值是有限的并且在设计时已知,在这种情况下,虽然属性是 int,但您可以为您的属性使用 Enum 转换器,而无需覆盖任何内容:

public class MyObject
{
    [TypeConverter(typeof(MyTypeConverter))]
    public int MyProperty { get; set; }
}
public class MyTypeConverter : EnumConverter
{
    public MyTypeConverter() : base(typeof(MyValues)) { }
}
public enum MyValues
{
    On = 1,
    Off,
    Unknown
}

创建您自己的支持标准值的 TypeConverter

如果您不能拥有枚举并且标准值是在运行时生成的,则可以创建这样的 TypeConverter:

public class MyTypeConverter : TypeConverter
{
    Dictionary<int, string> values;
    public MyTypeConverter()
    {
        values = new Dictionary<int, string> { { 1, "1 - On" }, { 2, "2 - Off" }, { 3, "3 - Unknown" } };
    }
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string)) return true;
        return base.CanConvertFrom(context, sourceType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value != null && values.ContainsValue(value.ToString()))
            return values.Where(x => x.Value == value.ToString()).FirstOrDefault().Key;
        return base.ConvertFrom(context, culture, value);
    }
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string) && value != null && value.GetType() == typeof(int))
            return values[(int)value];
        return base.ConvertTo(context, culture, value, destinationType);
    }
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }
    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return new StandardValuesCollection(values.Keys);
    }
}

关于c# - 如何格式化/更改属性网格中的显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39875274/

相关文章:

.net - 鼠标移动灵敏度

c# - 检查数组是否重复,只返回出现不止一次的项目

c# - 是否有三元运算符可以在 C# 中运行函数?

.net - WMB8/.NET 计算节点调试

c# - 带有大小参数的不成比例的气泡图

使用 Windows 窗体进行 C# 单元测试

c# - 正则表达式在模拟器中有效,但在 C# 实现中失败。我缺少什么?

c# - 检查是否安装了完整版本的 .net?

html - 通过 Controller CSS 访问 View 时不起作用

.net - 在数据库上运行测试的策略