C# SelectionIndex更改了两次点火

C# SelectionIndex更改了两次点火,c#,windows-phone-7,binding,listbox,C#,Windows Phone 7,Binding,Listbox,我想知道为什么当我点击列表中的一个项目时,我的选择索引被更改了两次 这是我在SelectionIndex中使用的代码 private void listBoxFolders_SelectionChanged(object sender, SelectionChangedEventArgs e) { // Taking the name of the folder to pass in the parameters if ((Folder)listBoxF

我想知道为什么当我点击列表中的一个项目时,我的选择索引被更改了两次

这是我在SelectionIndex中使用的代码

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

        // Connection 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 { }

        }

    }
public void wc_DownloadStringCompleted2(对象发送方,DownloadStringCompletedEventArgs e)
{
如果(e.Error==null)
{
XDocument xdoc=XDocument.Parse(例如,Result,LoadOptions.None);
XNamespace aNamespace=XNamespace.Get(“http://schemas.datacontract.org/2004/07/System.IO");
尝试
{
//检索子文件夹
var folders=来自xdoc.subjects中的查询(aNamespace.GetName(“DirectoryInfo”))
选择新文件夹
{
Name=(string)query.Element(“OriginalPath”),
};
_lFolders=新的可观察集合();
foreach(文件夹中的文件夹f)
{
l增加(f);
}
listBoxFolders.ItemsSource=L文件夹;
listBoxFolders.DisplayMemberPath=“Name”;
//检索文件
var files=来自xdoc.subjects中的查询(aNamespace.GetName(“FileInfo”))
选择新文件
{
Name=(string)query.Element(“OriginalPath”),
};
_lFiles=新的可观测集合();
foreach(文件中的文件f)
{
l.添加(f);
}
listBoxFiles.ItemsSource=LFiles;
listBoxFiles.DisplayMemberPath=“Name”;
listBoxFiles.SelectionChanged+=新的SelectionChangedEventHandler(listBoxFiles\u SelectionChanged);
}
捕获{}
}
}

选择更改事件发生时,您正在重新加载listbox的项目源。由于重新加载操作,索引将更改为其默认值,即,-1。 这可能是你的问题。 不使用选择更改事件,而使用点击事件