【问题标题】:C# Pass file path from file in listbox to dataconextC#将文件路径从列表框中的文件传递到dataconext
【发布时间】:2015-02-26 22:35:09
【问题描述】:

我正在用一个文件填充一个列表框。这可以通过两种方法来完成,由按下按钮启动的打开文件对话框命令和将操作拖放到列表框中。我想将文件路径(对于列表框中的文件)传递给我的代码的其他区域。比如读取列表框中文件的DataContext。基本上我希望在填充列表框时自动更新文件路径。如果我没有正确解释自己或提供足够的信息,我是 C# 新手,非常抱歉。填充我的列表框(名为 FilePathBox)和“运行”按钮的代码如下:

private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
    var openFileDialog = new Microsoft.Win32.OpenFileDialog();
    //openFileDialog.Multiselect = true;
    openFileDialog.Filter = "Csv files(*.Csv)|*.Csv|All files(*.*)|*.*";
    openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    if (openFileDialog.ShowDialog())
    {
        FilePathBox.Items.Clear();

        foreach (string filename in openFileDialog.FileNames)
        {
            ListBoxItem selectedFile = new ListBoxItem();

            selectedFile.Content = System.IO.Path.GetFileNameWithoutExtension(filename);
            selectedFile.ToolTip = filename;                    
            FilePathBox.Items.Add(selectedFile);
        }
    }          
}

private void FilesDropped(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        FilePathBox.Items.Clear();

        string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];

        foreach (string droppedFilePath in droppedFilePaths)
        {
            ListBoxItem fileItem = new ListBoxItem();

            fileItem.Content = System.IO.Path.GetFileNameWithoutExtension(droppedFilePath);
            fileItem.ToolTip = droppedFilePath;
            FilePathBox.Items.Add(fileItem);
        }
    }  
}

private void RunButton_Click(object sender, RoutedEventArgs e)
{
    DataContext = OldNewService.ReadFile(@"C:\Users\Documents\Lookup Table.csv");
}

【问题讨论】:

    标签: c# wpf listbox datacontext


    【解决方案1】:

    参考下面的代码。

    <StackPanel>            
            <ListBox x:Name="fileList" Height="100" Drop="fileList_Drop" DisplayMemberPath="Name" AllowDrop="True"/>
            <Button Content="Browse" Click="Button_Click"/>
            <Rectangle/>
        </StackPanel>
     public partial class MainWindow : Window
    {
        ObservableCollection<FileInfo> lst = new ObservableCollection<FileInfo>();
        public MainWindow()
        {
            InitializeComponent();
            fileList.ItemsSource = lst;
    
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            if ((bool)dlg.ShowDialog())
            {
                FileInfo fi= new FileInfo(dlg.FileName);
                lst.Add(fi);
            }
        }
    
        private void fileList_Drop(object sender, DragEventArgs e)
        {
            string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];
    
            foreach (string droppedFilePath in droppedFilePaths)
            {
                FileInfo fi = new FileInfo(droppedFilePath);
                lst.Add(fi);
            }
        }
    }
    

    【讨论】:

    • 这似乎对我不起作用。我希望能够将文件放入我的列表框中,点击我的运行按钮,然后我的程序使用列表框中文件的文件路径并完成对该文件的操作。可能我不是很清楚。本质上,我想自动填充以下部分的文件路径,包含在我上面的代码中:DataContext = OldNewService.ReadFile(@"C:\Users\Documents\Lookup Table.csv");
    • 我无法获得为我工作的答案。有人可以帮忙吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2019-03-15
    相关资源
    最近更新 更多