2012-04-17 85 views
1

我想知道为什么我的选择索引更改是在点击列表上的某个项目时发射两次。SelectionIndexChanged发射两次

这是我在selectionindexchanged

private void listBoxFolders_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     // Taking the name of the folder to pass in the parameters 
     if ((Folder)listBoxFolders.SelectedItem != null) 
     { 
      folderTmp = (Folder)listBoxFolders.SelectedItem; 
     } 

     // Connexion to the webservice to get the subfolders and also the files 
     WebClient wc = new WebClient(); 
     wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted2); 
     wc.DownloadStringAsync(new Uri("http://clients.uicentric.net/IISHostedCalcService/FilesService.svc/GetFoldersAndFiles?selectedFolder=" + folderTmp.Name)); 
    } 

使用的代码,这是方法至极的里面烧两次:

public void wc_DownloadStringCompleted2(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None); 
      XNamespace aNamespace = XNamespace.Get("http://schemas.datacontract.org/2004/07/System.IO"); 

      try 
      { 

       // Retrieving the subfolders 
       var folders = from query in xdoc.Descendants(aNamespace.GetName("DirectoryInfo")) 
           select new Folder 
           { 
            Name = (string)query.Element("OriginalPath"), 
           }; 

       _lFolders = new ObservableCollection<Folder>(); 

       foreach (Folder f in folders) 
       { 
        LFolders.Add(f); 
       } 

       listBoxFolders.ItemsSource = LFolders; 
       listBoxFolders.DisplayMemberPath = "Name"; 


       // Retrieving the files 
       var files = from query in xdoc.Descendants(aNamespace.GetName("FileInfo")) 
          select new File 
         { 
          Name = (string)query.Element("OriginalPath"), 
         }; 


       _lFiles = new ObservableCollection<File>(); 

       foreach (File f in files) 
       { 

        LFiles.Add(f); 
       } 

       listBoxFiles.ItemsSource = LFiles; 
       listBoxFiles.DisplayMemberPath = "Name"; 
       listBoxFiles.SelectionChanged += new SelectionChangedEventHandler(listBoxFiles_SelectionChanged); 

      } 
      catch { } 

     } 

    } 

回答

3

你这是在选择重装列表框的项目源改变了事件。由于重新加载操作,索引变为默认值,即-1。 这可能一定是你的问题。 而不是使用选择更改事件去点击事件。

+0

感谢您的帮助! – Kiwimoisi 2012-04-17 10:59:08