2016-08-22 66 views
0

我爲媒體播放器創建播放列表。爲什麼多個項目不能在列表框上工作?

按照我的代碼:

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

     } 
    } 
} 

它的工作單個文件下降,但同時,我去掉了多項文件,那麼它是不是在列表框中添加。

加載多個文件,但多個文件不會丟失。

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

+0

我複製粘貼你的代碼在一個新的WPF項目,並刪除多個文件「工作」,可以這麼說。你的代碼需要調整一下,但總體來說它確實「有效」。所以我不確定你面臨什麼問題? –

回答

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

     } 
    } 

這也將工作(使用for循環)

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]; 
     } 
    } 
+0

其工作正常。但**爲什麼是名單,並選擇最後一項?** –

+0

我不知道我理解你的問題。對於它的價值,ForEach和/或ForLoop可以輕鬆減少一點。 'filePath'和'fileName'沒有必要。 'listbox4.Items.Add(fileName)'是將項目添加到列表框的位置,'listbox4.SelectedItem = filename'是您在列表框中更改活動選擇的位置。 –

+0

如果只有下來的選民會留下評論,以配合他們的「投票」... –

相關問題