如何截断C#ListBox中的DisplayMember?

时间:2018-06-20 13:50:38

标签: c# text listbox truncate

我有一个winforms ListBox,它显示完整文件路径的列表。但是如您所见,Path属性太长了。如何在不向列表条目添加全新属性的情况下使ListBox仅显示文件名而不显示完整路径?

myListBox.DisplayMember = "Path";

enter image description here

2 个答案:

答案 0 :(得分:2)

如果将文件路径转换为System.IO.FileInfo对象,它将为您提供更多选择。

尝试一下:

string[] files = System.IO.Directory.GetFiles("C:\\tmp");
List<System.IO.FileInfo> fiList = new List<System.IO.FileInfo>();
foreach(string f in files)
    fiList.Add(new System.IO.FileInfo(f));
myListBox.DataSource = fiList;
myListBox.DisplayMember = "Name";
myListBox.ValueMember = "FullName";

答案 1 :(得分:1)

无需在列表项中添加全新的属性,就可以使用列表框的DrawItem事件(确保将DrawMode属性更改为OwnerDrawFixed)。

func getIndexPathFor(view: UIView, tableView: UITableView) -> IndexPath? {

            let point = tableView.convert(view.bounds.origin, from: view)
            let indexPath = tableView.indexPathForRow(at: point)
            return indexPath
 }