我建议您考虑使用 WPF。
使用 WPF,您将能够创建自己的 DataTemplate,告诉 ListBoxItem 仅显示 FileInfo.Name 属性,但“后面”它仍将设置所需的 FullPath 属性并准备使用...
另外,您可以在那里定义 SelectedItem,因此您将“自动”在 GUI 中的选择完成后立即实例化所需的对象。这样双击就可以实现,不需要很多编码工作。
这里的示例实现(对不起,只是快速,肮脏和丑陋......但是......)可以按照您的意愿工作。
XAML(CAVE:ListBox.ItemTemplate,您在其中告诉您希望在 TextBlock 中显示属性名称作为 ListBoxItem 的 DataTemplate,并且所选元素绑定到主窗口中的属性):
<Window x:Class="ListBoxFiles.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding FileList, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
SelectedItem="{Binding CurrentFile, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
MouseDoubleClick="ListBox_MouseDoubleClick">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Click="Button_Click" Content="browse" Grid.Row="1" Width="50" Height="30"/>
</Grid>
</Window>
C#(CAVE:使用 INotifyPropertyChanged,将您的 FileInfos 列表和 CurrentFile 定义为属性,双击事件的句柄)。我添加了简单的按钮来显示多选 FileOpenDialog 以便它可以填充列表...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
namespace ListBoxFiles
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnNotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private List<FileInfo> _fileList;
public List<FileInfo> FileList
{
get
{
return _fileList;
}
set
{
_fileList = value;
OnNotifyPropertyChanged("FileList");
}
}
private FileInfo _currentFile;
public FileInfo CurrentFile
{
get
{
return _currentFile;
}
set
{
_currentFile = value;
OnNotifyPropertyChanged("CurrentFile");
}
}
public MainWindow()
{
InitializeComponent();
}
private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//ListBox caller = (ListBox)sender;
//FileInfo fi = (FileInfo)caller.SelectedItem;
//Process.Start(fi.FullName);
Process.Start(this.CurrentFile.FullName);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All files (*.*)|*.*";
ofd.Multiselect = true;
bool dialogResult = (bool)ofd.ShowDialog();
if (dialogResult)
{
this._fileList = new List<FileInfo>();
FileInfo fi;
foreach (string filename in ofd.FileNames)
{
fi = new FileInfo(filename);
this._fileList.Add(fi);
}
OnNotifyPropertyChanged("FileList");
}
}
}
}