c# - PropertyGrid - 动态加载下拉值
问题描述
我已经尝试了各种方法——很多都在使用 TypeConverters 等。所以我不会在这里介绍所有这些。
因此,将问题简化为基础。考虑到以下情况:
Imports LiteDB
Imports System.ComponentModel
Public Class mSystem
<CategoryAttribute("General"), ReadOnlyAttribute(True)>
Public Property ID As Integer
Public Property ShortName As String = ""
<BsonRef("mSystemTypes")>
Public Property SystemType As mSystemType
End Class
Public Class mSystemType
<CategoryAttribute("General"), ReadOnlyAttribute(True)>
Public Property ID As Integer
Public Property Name As String = "Default"
Public Property Details As String = ""
End Class
如何将“SystemTypes”作为从 mSystemTypes 集合动态填充的下拉选择器?例如,您选择“控制台”,它会使用匹配的 mSystemType 更新 mSystem。
我正在使用 LiteDb,这可能会使事情变得更加困难,因为它不是像其他场景可能存在的“外键”纯粹的整数,而是一个完整的对象。
我需要维护“dbRef”方法以确保数据关系的完整性。以防万一 DBLite 东西抛出一个曲线球,下面的一些代码演示了它的使用:
Public Class dbCollecitons
Public mSystems As LiteCollection(Of mSystem)
Public mSystemTypes As LiteCollection(Of mSystemType)
Private Sub Examples()
Dim col As LiteCollection(Of mSystemType) = dbCollections.mSystemTypes
Dim value as String = "Console"
Dim q = col.FindAll.Where(Function(x) x.Name = value).First
Console.Writeline(q.ID)
End Sub
End Class
LiteDb.LiteCollection 不直接映射到 ICollection(你在 TypeConverter 中使用它?),但我确信有一些解决方法。
解决方案
简而言之,您需要创建一个新的TypeConverter
支持标准值。
示例 - VB.NET
我想你有一个Product
类,有一个类型的属性Category
,你希望能够从List<Category>
运行时来自数据库之类的某个地方进行选择:
Public Class Product
Public Property Id As Integer
Public Property Name As String
<TypeConverter(GetType(CategoryConverter))>
Public Property Category As Category
End Class
Public Class Category
Public Property Id As Integer
Public Property Name As String
Public Overrides Function ToString() As String
Return $"{Id} - {Name}"
End Function
End Class
这是CategoryService
可以从您喜欢的任何地方加载类别的类:
Public Class CategoryService
Private list As List(Of Category) = New List(Of Category) From {
New Category() With {.Id = 1, .Name = "Category 1"},
New Category() With {.Id = 2, .Name = "Category 2"},
New Category() With {.Id = 3, .Name = "Category 3"}
}
Public Function GetAll() As IEnumerable(Of Category)
Return list
End Function
End Class
下一步是创建CategoryConverter
负责为下拉列表提供值的对象:
Imports System.ComponentModel
Public Class CategoryConverter
Inherits TypeConverter
Public Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As StandardValuesCollection
Dim svc = New CategoryService()
Return New StandardValuesCollection(svc.GetAll().ToList())
End Function
Public Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
If sourceType = GetType(String) Then Return True
Return MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object
If value IsNot Nothing AndAlso value.[GetType]() = GetType(String) Then
Dim v = $"{value}"
Dim id = Integer.Parse(v.Split("-"c)(0).Trim())
Dim svc = New CategoryService()
Return svc.GetAll().Where(Function(x) x.Id = id).FirstOrDefault()
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
End Class
结果,当您设置Product
asSelectedObject
的PropertyGrid
, forCategory
属性的实例时,您从列表中选择一个值。
示例 - C#
这是上述示例的 C# 版本:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
[TypeConverter(typeof(CategoryConverter))]
public Category Category { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return $"{Id} - {Name}";
}
}
public class CategoryService
{
List<Category> list = new List<Category>{
new Category() { Id = 1, Name = "Category 1" },
new Category() { Id = 2, Name = "Category 2" },
new Category() { Id = 3, Name = "Category 3" },
};
public IEnumerable<Category> GetAll()
{
return list;
}
}
public class CategoryConverter : TypeConverter
{
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
var svc = new CategoryService();
return new StandardValuesCollection(svc.GetAll().ToList());
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
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, CultureInfo culture, object value)
{
if (value != null && value.GetType() == typeof(string))
{
var v = $"{value}";
var id = int.Parse(v.Split('-')[0].Trim());
var svc = new CategoryService();
return svc.GetAll().Where(x => x.Id == id).FirstOrDefault();
}
return base.ConvertFrom(context, culture, value);
}
}
推荐阅读
- systemd-journald - journalctl --after-cursor 不适用于 shell 脚本
- spring - 无法从 [java.lang.String] 转换为 [com.example.demo.User]
- linux - GNU Make 通配符函数和目录名称的终止斜杠
- php - 在自定义 WordPress 函数中渲染 Twig 文件
- javascript - 我应该寻找什么 Javascript / jQuery 事件来检测由 Javascript / jQuery 以编程方式填充的表单字段?
- kubernetes - 什么是 Kubernetes 引擎(RKE、GKE、EKS...)?
- python - Django - 如何制作多个自连接表?
- javascript - 使用查找或包含(赛普拉斯、节点、Javascript)时,来自 API 响应的数组不会解析为数组
- json - 如何使用 PL/SQL 将 JSON 数据解析成列?
- python - cython 在 cythonize 之后选择基于 Python 的实现