为什么多项目掉线不在列表框上?

时间:2016-08-22 15:13:25

标签: c# wpf

我为媒体播放器创建播放列表。

按照我的代码:

的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;
            }

        }
    }
}

它正在运行单个文件丢弃,但是当我删除多个文件时,它没有添加到列表框中。

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

如何在列表框中删除多个文件?

1 个答案:

答案 0 :(得分:1)

由于我无法复制您列出的问题,因此我目前无法帮助您。虽然,我可以在我觉得合适的地方帮助你。

您当前的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];
        }
    }