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

时间:2013-06-19 22:53:28

标签: c# wpf listbox expander

此类(如下所示)是自定义组件。它应该只需通过点击扩展器内的列表来搜索文件夹。

ExpanderList是扩展器中的列表

DirMaker是实际扩展器

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

    }

}
}

它的效果非常好,除了私有的void ListClicked(对象发送者,SelectionChangedEventArgs e)  有时会触发更多一次,其效果是跳过更深的文件,然后按照预期的一个级别。当我在if上放置一个断点时,这种行为似乎没有重复  (ExpanderList.SelectedIndex!= -1)总是返回-1的行,如果不应该发生则阻止它。我已经看到有证据表明这种方法有一个错误吗?虽然我没有经验或前面指责别人.....它绝对是我虽然这个选项一直在我心中玩,因为我不能找到重复射击的原因。

真的至关重要,因为它让我疯狂

提前感谢您的任何帮助

约翰哈里斯

1 个答案:

答案 0 :(得分:0)

直接使用 ListBox.Items 属性不是一个好主意:您应首先在临时集合中运行文件夹检索,这是一个简单的List&lt; string&gt; ,一旦完成,您将更新ListBox 批量

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

至少这将是更优化,但检查是否也解决了您的问题。 :)