1

このクラス (以下) はカスタム コンポーネントです。エキスパンダー内のリストをクリックしてフォルダーを検索するだけです。

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

    }

}
}

それは本当にうまく機能します.1回以上発火することがあるprivate void ListClicked(object sender, SelectionChangedEventArgs e)は別として、意図した1レベルよりも深いファイルにスキップする効果があります. この動作は、if (ExpanderList.SelectedIndex != -1) 行にブレークポイントを配置しても繰り返されないようで、常に -1 を返し、発生しない場合はこれを防ぎます。この方法にバグがあることを示唆する証拠を見たことがありますか?, 私は他の誰かを非難する経験や正面を持っていませんが..繰り返し発砲する理由を見つけます。

非常に重要です。私はこの問題をできるだけ早く解決し、私を夢中にさせます

事前に助けてくれてありがとう

ジョン・ハリス

4

1 に答える 1