我想拿一个对象,比方说这个对象:
public class BenchmarkList
{
public string ListName { get; set; }
public IList<Benchmark> Benchmarks { get; set; }
}
并让该对象显示其 ListName 作为 PropertiesGrid 的“名称”部分(“Benchmark”会很好),并且对于 PropertyGrid 的“值”部分,有一个下拉列表的 IList<> 基准:
这是基准对象
public class Benchmark
{
public int ID {get; set;}
public string Name { get; set; }
public Type Type { get; set; }
}
我希望下拉菜单显示用户可以看到的基准的名称属性。这是一个视觉示例:
因此,从本质上讲,我试图将 Benchmark 对象的集合放入下拉列表中,并且这些对象应将其 Name 属性显示为下拉列表中的值。
我读过其他关于使用 PropertiesGrid 的文章,包括 THIS和 THIS ,但它们比我尝试做的更复杂。
我通常处理服务器端的东西,不通过 WebForms 或 WinForms 处理 UI,所以这个 PropertiesGrid 真的带我去兜风......
我知道我的解决方案在于实现“ICustomTypeDescriptor”,这将允许我告诉 PropertiesGrid 它应该显示什么值,而不管我要绑定(bind)到下拉列表中的对象的属性,但是我只是不确定如何或在哪里实现它。
任何指点/帮助将不胜感激。
谢谢, 迈克
更新:
好的,所以我稍微更改了一些细节。我之前对我认为应该涉及的对象太过分了,所以这是我的新方法。
我有一个名为 Analytic 的对象。这是应该绑定(bind)到 PropertiesGrid 的对象。现在,如果我公开一个枚举类型的属性,PropertiesGrid 将为我处理下拉列表,这非常好。如果我公开一个作为自定义类型集合的属性,PropertiesGrid 就不太好...
这是 Analytic 的代码,我想绑定(bind)到 PropertiesGrid 的对象:
public class Analytic
{
public enum Period { Daily, Monthly, Quarterly, Yearly };
public Analytic()
{
this.Benchmark = new List<IBenchmark>();
}
public List<IBenchmark> Benchmark { get; set; }
public Period Periods { get; set; }
public void AddBenchmark(IBenchmark benchmark)
{
if (!this.Benchmark.Contains(benchmark))
{
this.Benchmark.Add(benchmark);
}
}
}
这是实现 IBenchmark 接口(interface)的两个对象的简短示例:
public class Vehicle : IBenchmark
{
public Vehicle()
{
this.ID = "00000000-0000-0000-0000-000000000000";
this.Type = this.GetType();
this.Name = "Vehicle Name";
}
public string ID {get;set;}
public Type Type {get;set;}
public string Name {get;set;}
}
public class PrimaryBenchmark : IBenchmark
{
public PrimaryBenchmark()
{
this.ID = "PrimaryBenchmark";
this.Type = this.GetType();
this.Name = "Primary Benchmark";
}
public string ID {get;set;}
public Type Type {get;set;}
public string Name {get;set;}
}
这两个对象将被添加到 WinForms 代码中 Analytic 对象的 Benchmark List 集合中:
private void Form1_Load(object sender, EventArgs e)
{
Analytic analytic = new Analytic();
analytic.AddBenchmark(new PrimaryBenchmark());
analytic.AddBenchmark(new Vehicle());
propertyGrid1.SelectedObject = analytic;
}
这是 PropertiesGrid 中输出的屏幕截图。请注意,作为枚举公开的属性获得了一个没有任何作用的漂亮下拉列表,但作为 List on 公开的属性获得了 (Collection) 的值。当你点击(Collection)时,你会得到 Collection 编辑器,然后可以看到每个对象,以及它们各自的属性:
这不是我要找的。就像我在这篇文章中的第一个屏幕抓取一样,我试图将 List 的属性 Benchmark 集合呈现为一个下拉列表,该列表将对象的名称属性显示为可以显示的文本...
谢谢
最佳答案
通常,属性网格中的下拉列表用于设置给定列表中的属性值。在这里,这意味着您最好拥有 IBenchmark 类型的“Benchmark”之类的属性,以及其他地方可能的 IBenchmark 列表。我冒昧地像这样更改您的分析类:
public class Analytic
{
public enum Period { Daily, Monthly, Quarterly, Yearly };
public Analytic()
{
this.Benchmarks = new List<IBenchmark>();
}
// define a custom UI type editor so we can display our list of benchmark
[Editor(typeof(BenchmarkTypeEditor), typeof(UITypeEditor))]
public IBenchmark Benchmark { get; set; }
[Browsable(false)] // don't show in the property grid
public List<IBenchmark> Benchmarks { get; private set; }
public Period Periods { get; set; }
public void AddBenchmark(IBenchmark benchmark)
{
if (!this.Benchmarks.Contains(benchmark))
{
this.Benchmarks.Add(benchmark);
}
}
}
您现在需要的不是 ICustomTypeDescriptor,而是 TypeConverter 和 UITypeEditor。您需要使用 UITypeEditor(如上)装饰 Benchmark 属性,并使用 TypeConverter 装饰 IBenchmark 接口(interface),如下所示:
// use a custom type converter.
// it can be set on an interface so we don't have to redefine it for all deriving classes
[TypeConverter(typeof(BenchmarkTypeConverter))]
public interface IBenchmark
{
string ID { get; set; }
Type Type { get; set; }
string Name { get; set; }
}
这是一个示例 TypeConverter 实现:
// this defines a custom type converter to convert from an IBenchmark to a string
// used by the property grid to display item when non edited
public class BenchmarkTypeConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
// we only know how to convert from to a string
return typeof(string) == destinationType;
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (typeof(string) == destinationType)
{
// just use the benchmark name
IBenchmark benchmark = value as IBenchmark;
if (benchmark != null)
return benchmark.Name;
}
return "(none)";
}
}
这是一个示例 UITypeEditor 实现:
// this defines a custom UI type editor to display a list of possible benchmarks
// used by the property grid to display item in edit mode
public class BenchmarkTypeEditor : UITypeEditor
{
private IWindowsFormsEditorService _editorService;
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
// drop down mode (we'll host a listbox in the drop down)
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
_editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
// use a list box
ListBox lb = new ListBox();
lb.SelectionMode = SelectionMode.One;
lb.SelectedValueChanged += OnListBoxSelectedValueChanged;
// use the IBenchmark.Name property for list box display
lb.DisplayMember = "Name";
// get the analytic object from context
// this is how we get the list of possible benchmarks
Analytic analytic = (Analytic)context.Instance;
foreach (IBenchmark benchmark in analytic.Benchmarks)
{
// we store benchmarks objects directly in the listbox
int index = lb.Items.Add(benchmark);
if (benchmark.Equals(value))
{
lb.SelectedIndex = index;
}
}
// show this model stuff
_editorService.DropDownControl(lb);
if (lb.SelectedItem == null) // no selection, return the passed-in value as is
return value;
return lb.SelectedItem;
}
private void OnListBoxSelectedValueChanged(object sender, EventArgs e)
{
// close the drop down as soon as something is clicked
_editorService.CloseDropDown();
}
}
关于c# - 在 PropertiesGrid 中将自定义对象列表显示为下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5171037/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c