CheckedListBox - FileInfo 对象的集合 - 显示自定义 ToString
我在 WinForms 应用程序(3.5 运行时)中有一个 CheckedListBox,并且我将一堆 FileInfo 对象添加到 Items ObjectCollection 中。问题是我不喜欢 CheckedListBox 中显示的内容(因为 FileInfo 来自 Directory.GetFiles() 它只显示列表框中文件的 FileInfo.Name)。
有没有简单的方法可以更改 CheckedListBox 中显示的内容,而无需创建单独的自定义类/对象。
我基本上是在执行
checkedListBox.Items.Add(fileInfo)
,结果只是文件的文件名。
更改显示成员有效,但我无法创建自定义内容,只能创建 FileInfo 类中的现有属性。
我希望能够显示类似名称 - 全名
示例(所需)的内容: File1.txt - C:\Path\SubPath\File1.txt
I have a CheckedListBox in a WinForms app (3.5 runtime), and I am adding a bunch of FileInfo objects to the Items ObjectCollection. The problem is that I don't like what is displayed in the CheckedListBox (since the FileInfo was from a Directory.GetFiles() it just shows the FileInfo.Name of the file in the listbox).
Is there any easy way to change what is displayed in the CheckedListBox without having to create a seperate custom class/object.
I am basically doing
checkedListBox.Items.Add(fileInfo)
and the result is just the file name of the file.
Changing display member works but I can't create something custom, only the existing properties in the FileInfo class.
I want to be able to display something like Name - FullName
Example (desired):
File1.txt - C:\Path\SubPath\File1.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,看起来应该是可能的。
CheckedListBox
有一个FormattingEnabled
属性和一个继承自ListBox
的Format
事件,该事件在显示每个项目之前调用。因此,沿着这些思路的东西应该有效:但还没有测试过。另请参阅 MSDN
旧回答:
我认为如果不创建包装器就无法做到这一点。虽然 10 行代码对我来说似乎并没有那么糟糕:
拥有视图模型的额外优势是您可以按照您喜欢的方式进一步装饰它以用于显示目的。
Actually, it seems like it should be possible after all. The
CheckedListBox
has aFormattingEnabled
property and aFormat
event inherited fromListBox
which is called before each item is displayed. So something along these lines should work:Haven't tested it though. See also MSDN
Old answer:
I don't think you can do it without creating a wrapper. Although 10 lines of code don't seem all that bad to me:
The additional advantage to having a view model is that you can decorate it further for display purposes all the way you like.
我不知道除了创建一个
custom
类并在其中包含一个FileInfo
实例之外是否有解决方法通过这种方式,您可以创建一个新的
属性
并在其中包含自定义数据,或者覆盖
ToString()
函数,例如 (这用于演示目的)
I dont know if there is work around for this except creating a
custom
class and include an instance ofFileInfo
inside itand in this way you either create a new
property
and include a custom data in it oroverride
theToString()
functionsomething like (this for demonstration purposes)