CSharp - 为什么我的wpf自定义组件列表框触发两次?

  显示原文与译文双语对照的内容
0 0

此类( 下方) 是自定义组件。 它只需要通过点击扩展器内的List 来搜索文件夹。

ExpanderList是扩展器中的List

DirMaker是actuall扩展器


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace BigHand
{
///<summary>
///Interaction logic for DirMakerControl.xaml
///</summary>
public partial class DirMakerControl : UserControl
{
 string currentDirectory ="c:/"; 
 DirectoryInfo di;
 bool directoryEmpty;

 public DirMakerControl()
 {
 this.InitializeComponent();

 getFolders();
 updateHeader();
 }

 private void getFolders()
 {
 ExpanderList.Items.Clear();
 try
 {
 di = new DirectoryInfo(currentDirectory);

 foreach (DirectoryInfo d in di.GetDirectories())
 {
 ExpanderList.Items.Add(d.ToString());
 }

 }
 catch (UnauthorizedAccessException)
 {
//goBack() keeps display the same
 goBack();
 MessageBox.Show(" SORRY!!!! nYou dont have the correct npermisions to use these files.");
 }

 if (ExpanderList.Items.Count == 0)
 {
 directoryEmpty = true;
 ExpanderList.IsEnabled = false;
 ExpanderList.Items.Add("No Folders Here");
 }
 else
 {
 directoryEmpty = false;
 ExpanderList.IsEnabled = true;
 }

 updateHeader();
 }

 private void ListClicked(object sender, SelectionChangedEventArgs e)
 {
//If selected index is -1 (no selection) do nothing 
 if (ExpanderList.SelectedIndex!= -1)
 {

//update list and variables
 if (!directoryEmpty)
 {
 ExpanderList.IsEnabled = true;
 currentDirectory = currentDirectory + ExpanderList.SelectedItem +"/";

//Reset selected index to -1 (no selection) 
 ExpanderList.SelectedIndex = -1;

 getFolders();
 }
 }

 }

 private void goBack()
 {
 currentDirectory = currentDirectory.Substring(0, getCurrentFolderStringIndex());

 getFolders();
 }

 private void Button_Click(object sender, RoutedEventArgs e)
 {
 goBack();
 }

//returns an integer representing where the current folder name starts in the current folder string
 public int getCurrentFolderStringIndex()
 {
 char[] currentD = currentDirectory.ToCharArray();

 for (int index = currentD.Length - 2; index> = 0; index--)
 {
 if (currentD[index] == '/')
 {
 return index + 1; 
 }
 }

//if this gets called(should at least start back at the c:/)
 return 0;

 }

 private void updateHeader()
 {
//Change Header
//get temp int to save calling the method twice
 int index = getCurrentFolderStringIndex();

 if (index == 0) 
 DirMaker.Header = currentDirectory; 
 else
 DirMaker.Header = currentDirectory.Substring(index, currentDirectory.Length - index);
 }

 private void selectionMade(ListBox sender, System.Windows.Input.MouseButtonEventArgs e)
 {
//TODO: Add event handler implementation here.
//If selected index is -1 (no selection) do nothing 
 if (ExpanderList.SelectedIndex == -1)
 return;

//update list and variables
 if (!directoryEmpty)
 {
 ExpanderList.IsEnabled = true;
 currentDirectory = currentDirectory + ExpanderList.SelectedItem +"/";

//Reset selected index to -1 (no selection) 
 ExpanderList.SelectedIndex = -1;

 getFolders();
 } 

 }

}
}

它很好地工作,除了 private void ListClicked(object sender, SelectionChangedEventArgs e) 之外,有时会激发更多的事情,这一点会让一次跳转到更深的文件,然后。 当我在( ExpanderList 。宋体!= -1 ) 行上放置断点时,这个行为似乎不会重复,如果它的NOT 应该会发生,那么它就会返回 -1. 我看到证据表明有一个 Bug 有这个方法虽然我做了 NOT的经验还是accuse了。。 肯定我尽管这个选项一直在我脑海中更多的像我 NOT 找个理由可以多次射击。

很重要的是,我得尽快找到它的驱动我疯狂

感谢你的帮助

harris

时间: 原作者:

0 0

直接使用 ListBox.Items 属性不是一个好主意: 你应该首先运行的文件夹检索临时集合,一个简单的List <string>,一旦你完成更新列表框散装 :


ExpanderList.Items.Clear();
foreach (string s in tmp)
{
 ExpanderList.Items.Add(s);
}

至少这会更加优化,但是要检查一下是否能解决你的问题。 : )

原作者:
...