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
Actually, it seems like it should be possible after all. The
CheckedListBoxhas aFormattingEnabledproperty and aFormatevent inherited fromListBoxwhich 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.