본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

[자작] WPF & C# - 폴더트리를 StackPanel 로 구성

728x90
반응형


 [자작] WPF & C# - 폴더트리를 StackPanel 로 구성



StackPanelScrolling.zip



<전체화면>


<축소사이즈>




MainWindow.xaml



1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Canvas x:Name="canvas">
<StackPanel x:Name="stpOrigan">
    <i:Interaction.Behaviors>
        <ei:FluidMoveBehavior Duration="0:0:0.5">
            <ei:FluidMoveBehavior.EaseY>
                <QuinticEase EasingMode="EaseInOut"/>
            </ei:FluidMoveBehavior.EaseY>
            <ei:FluidMoveBehavior.EaseX>
                <CircleEase EasingMode="EaseOut"/>
            </ei:FluidMoveBehavior.EaseX>
        </ei:FluidMoveBehavior>
    </i:Interaction.Behaviors>
</StackPanel>
</Canvas>
cs



MainWindow.xaml.cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
public MainWindow()
{
    InitializeComponent();
 
    dirSearch(@".\폴더트리\");
}
 
int imageCount = 0;   // 이미지 총 개수
private void dirSearch(string dir)
{
    // 폴더에 따라 StackPanle 생성
    StackPanel stp = new StackPanel();
    stp.Orientation = Orientation.Horizontal;
 
    string[] Directories = Directory.GetDirectories(dir);   // Defalut Folder
 
    // File
    string[] Files = Directory.GetFiles(dir);   // File list Search
    foreach (string fileName in Files)   // File check
    {
        // 이미지 생성
        Image img = new Image();
        img.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + fileName, UriKind.RelativeOrAbsolute));
        img.Stretch = Stretch.UniformToFill;
        img.Tag = imageCount;
        img.MouseLeftButtonDown += (s, a) =>
        {
            Search(int.Parse(img.Tag.ToString()) + 1);
        };
 
        // 라벨생성 (넘버링 체크)
        Label lbl = new Label();
        lbl.Content = img.Tag.ToString();
        lbl.Height = 10;
 
        // Grid 생성
        Grid grid = new Grid();
        grid.Tag = imageCount;
        grid.Width = 100;
        grid.Height = 100;
        grid.Children.Add(img);
        grid.Children.Add(lbl);
 
        stp.Children.Add(grid);
        imageCount++;
    }
    stpOrigan.Children.Add(stp);
 
    // Folder
    foreach (string nodeDir in Directories)   // Folder list Search
    {
        dirSearch(nodeDir);   // reSearch
    }
}
 
// 이동 대상 찾기
StackPanel stackPanel = null;   // 대상 포함여부 확인
Grid grdSelect = null;
void Search(int index)
{
    index = index % imageCount;   // 끝까지 가면 다시 0부터 시작
    try
    {
        // 해당 grid 찾기
        for (int i = 0; i < stpOrigan.Children.Count; i++)   // stackPanel 전체 찾기
        {
            stackPanel = stpOrigan.Children.OfType<StackPanel>().ElementAt(i);   
            grdSelect = stackPanel.Children.OfType<Grid>().FirstOrDefault(r => int.Parse(r.Tag.ToString()) == index);   // 해당 stackPanel 에 대상포함여부 확인
            if (grdSelect != nullbreak;   // 대상을 찾았을경우 스탑~!
        }
 
        // 해당 포인트로 이동
        Point ptStart = stpOrigan.TranslatePoint(new Point(00), canvas);   // 전체 StackPanel 담고 있는 객체
        Point ptTarget = grdSelect.TranslatePoint(new Point(00), canvas);   // 해당 Grid 객체
        Canvas.SetLeft(stpOrigan, ptStart.X - ptTarget.X);   // 전체 이동
        Canvas.SetTop(stpOrigan, ptStart.Y - ptTarget.Y);   // 전체 이동
    }
    catch { }
    finally
    {
        stackPanel = null;
        grdSelect = null;
    }
}
cs












728x90
반응형