2016-08-22 7 views
0

私はメディアプレーヤーのプレイリストを作成します。これは、単一のファイルのドロップを働いて複数のアイテムドロップがリストボックスで機能しないのはなぜですか?

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

     } 
    } 
} 

が、私は、複数のファイルをドロップしながら、それがリストボックスに追加されていないです:

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

は私のコードに従ってください。

複数のファイルが読み込まれていますが、複数のファイルは削除されません。

リストボックスに複数のファイルをドロップするにはどうすればよいですか?

+0

新しいWPFプロジェクトにコードを貼り付け、複数のファイルをドロップすると、「うまくいった」と言われました。あなたのコードは微調整が必​​要ですが、全体的には "うまくいっています"。だからあなたはどんな問題に直面しているのか分かりません。 –

答えて

1

私が問題を再現できないため、現在私はその点であなたを助けることはできません。しかし、私はあなたが合うと思うところであなたを助けることができます。

現在のDropメソッドには、リストボックスに追加するアイテムの数を倍にする余分なループがあります。

あなたの現在の方法:(のForEachを使用して)

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 

      } 
     } 
    } 

リファクタリング

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

選択を維持するか、または最後を選択する

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

選択された項目がある場合、選択された項目を維持するために、リスト

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

ダウン投票者だけが「投票」に同意するコメントを残していたら... –

関連する問題