为什么是多个项目不工作的列表框

本文关键字:工作 列表 项目 为什么 | 更新日期: 2023-09-27 18:03:23

我创建了一个媒体播放器播放列表。

遵循我的代码:

Xaml:

<MediaElement x:Name="mePlayer" Margin="64,0,90,61"  ></MediaElement>
<ListBox x:Name="listbox4" Background="Salmon"   BorderBrush="Black" BorderThickness="3" Drop="listbox4_Drop"  >
</ListBox>
<Button x:Name="load" Content="Load" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="76" Click="load_Click" Margin="184,285,0,0"/>

Xaml.cs :

private Dictionary<string, string> fileDictionary = new Dictionary<string, string>();   
private void load_Click(object sender, RoutedEventArgs e)
{
    Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
    ofd.DefaultExt = ".mp3";
    ofd.Filter = "All|*.*";
    ofd.Multiselect = true;
    Nullable<bool> result = ofd.ShowDialog();
    if (result == true)
    {
        for (int i = 0; i < ofd.FileNames.Length; i++)
        {
            var filePath = ofd.FileNames[i];
            var fileName = System.IO.Path.GetFileName(filePath);
            fileDictionary.Add(fileName, filePath);
            listbox4.Items.Add(fileName);
            listbox4.SelectedItem = fileName;
        }
    }
}

private void listbox4_Drop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] droppedFilePaths =
            e.Data.GetData(DataFormats.FileDrop, true) as string[];
        foreach (string droppedFilePath in droppedFilePaths)
        {
         for (int i = 0; i < droppedFilePaths.Length; i++)
            {
                var filePath = droppedFilePaths[i];
                var fileName = System.IO.Path.GetFileName(filePath);
                fileDictionary.Add(fileName, filePath);
                listbox4.Items.Add(fileName);
                listbox4.SelectedItem = fileName;
            }
        }
    }
}

它是工作的单一文件,但当我删除多个文件,然后它不会添加到列表框。

加载多个文件,但不删除多个文件。

我如何在列表框上放置多个文件?

为什么是多个项目不工作的列表框

由于我无法复制您列出的问题,因此我目前无法在这方面帮助您。不过,我可以在我认为合适的地方帮助你。

你当前的Drop方法有一个额外的循环,可以将你添加到列表框中的项数乘以。

当前方法:

    private void listbox4_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];
            foreach (string droppedFilePath in droppedFilePaths)
            {
                //if you keep this loop, you will all the dropped files for each dropped file
                //therefore, if I dropped 3 files, I'd get 9 entries in the listbox
                //if I dropped 4 files, I'd get 16 entries and so on...
                for (int i = 0; i < droppedFilePaths.Length; i++)//this has to go
                {//this has to go
                    var filePath = droppedFilePaths[i];//this needs to be a different variable since "i" will no longer exist
                    var fileName = System.IO.Path.GetFileName(filePath);
                    //fileDictionary.Add(fileName, filePath);
                    listbox4.Items.Add(fileName);
                    listbox4.SelectedItem = fileName;
                }//this has to go
            }
        }
    }

重构(使用ForEach)

    private void blaze_125_listbox4_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];
            foreach (string droppedFilePath in droppedFilePaths)
            {
                var filePath = droppedFilePath;
                var fileName = System.IO.Path.GetFileName(filePath);
                //fileDictionary.Add(fileName, filePath);
                listbox4.Items.Add(fileName);
                listbox4.SelectedItem = fileName;
            }
        }
    }

这也可以(使用ForLoop)

    private void blaze_125_listbox4_Drop_anotherSpin(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];
            for (int i = 0; i < droppedFilePaths.Length; i++)
            {
                var filePath = droppedFilePaths[i];
                var fileName = System.IO.Path.GetFileName(filePath);
                //fileDictionary.Add(fileName, filePath);
                listbox4.Items.Add(fileName);
                listbox4.SelectedItem = fileName;
            }
        }
    }

苗条

    private void blaze_125_listbox4_Drop_Slimmer(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];
            foreach (string droppedFilePath in droppedFilePaths)
            {
                listbox4.Items.Add(System.IO.Path.GetFileName(droppedFilePath));
            }
        }
    }

使用字典存储项,并更新列表

    Dictionary<string, string> fileDictionary = new Dictionary<string, string>();
    private void blaze_125_listbox4_Drop_Slimmer(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];
            foreach (string droppedFilePath in droppedFilePaths)
            {
                //listbox4.Items.Add(System.IO.Path.GetFileName(droppedFilePath));//don't need this anymore
                //Check if the file is already in the dictionary.
                //Check by looking up the key, and by looking up the value too.
                if (fileDictionary.ContainsKey(System.IO.Path.GetFileName(droppedFilePath)) || fileDictionary.ContainsValue(droppedFilePath))
                {
                    //no need to add this file, it's already in the dictionary
                    //if you try to add a file with a KEY identical to a KEY that already exists in the dictionary,
                    //it will throw an exception
                    //A dictionary can contain the same value multiple times, but it can not contain the same key more than once.
                }
                else
                {
                    //the file is not listed in the dictionary, so lets add it
                    fileDictionary.Add(System.IO.Path.GetFileName(droppedFilePath), droppedFilePath);
                }
            }
        }
        //Now lets call the method in charge of updating the listbox 
        UpdateTheListbox(fileDictionary, listbox4);
    }
    private void UpdateTheListbox(Dictionary<string, string> incomingDictionary, ListBox listboxToModify)
    {
        listboxToModify.Items.Clear();//clear all the items in the list
        foreach (KeyValuePair<string, string> item in incomingDictionary)
        {
            listboxToModify.Items.Add(item.Key);
        }
        //this method should probably be optimized because if your listBox already contains a large number of items
        //it may be quicker to only add the missing items, instead of reverting back to an empty list, and adding all the items to it again.
        //Though I'll leave this up to you to implement. We'll be here to answer questions if you have a hard time doing it.
    }

更新了在存在选定项时维护选定项的方法

    private void UpdateTheListboxMaintainExistingSelection(Dictionary<string, string> incomingDictionary, ListBox listboxToModify)
    {
        var preSelectedItem = listboxToModify.SelectedItem;//store the current selection
        listboxToModify.Items.Clear();//clear all the items in the list
        foreach (KeyValuePair<string, string> item in incomingDictionary)
        {
            listboxToModify.Items.Add(item.Key);
        }
        //this method should probably be optimized because if your listBox already contains a large number of items
        //it may be quicker to only add the missing items, instead of reverting back to an empty list, and adding all the items to it again.
        //Though I'll leave this up to you to implement. We'll be here to answer questions if you have a hard time doing it.
        //Maintain the selected item if there was one
        if (preSelectedItem != null)
        {
            listboxToModify.SelectedItem = preSelectedItem;
        }
    }

保持选中项,如果没有选中项,则选择最后一项

    private void UpdateTheListboxMaintainExistingOrSelectLastAdded(Dictionary<string, string> incomingDictionary, ListBox listboxToModify)
    {
        var preSelectedItem = listboxToModify.SelectedItem;//store the current selection
        listboxToModify.Items.Clear();//clear all the items in the list
        foreach (KeyValuePair<string, string> item in incomingDictionary)
        {
            listboxToModify.Items.Add(item.Key);
        }
        //this method should probably be optimized because if your listBox already contains a large number of items
        //it may be quicker to only add the missing items, instead of reverting back to an empty list, and adding all the items to it again.
        //Though I'll leave this up to you to implement. We'll be here to answer questions if you have a hard time doing it.

        if (preSelectedItem != null)
        {
            //Maintain the selected item if there was one
            listboxToModify.SelectedItem = preSelectedItem;
        }
        else
        {
            //select the last item in the listbox if nothing was pre-selected
            listboxToModify.SelectedItem = listboxToModify.Items[listboxToModify.Items.Count - 1];
        }
    }