예제 #1
0
        /// <summary>Navigates to a specified directory.</summary>
        /// <param name="directory">The directory.</param>
        private void NavigateTo(string directory)
        {
            if (string.IsNullOrEmpty(directory))
                throw new ArgumentNullException("directory");

            FileView fileView = new FileView(directory);
            string currentDirectory = _oldDirectory;
            _oldDirectory = fileView.FullName;

            if (!_drivesAdded)
            {
                _drivesAdded = true;

                foreach (DriveInfo drive in DriveInfo.GetDrives())
                {
                    DirectoryTreeNode node = new DirectoryTreeNode(0);
                    node.Expanding += treeView1_BeforeExpand;
                    node.Collapsing += treeView1_BeforeCollapse;
                    treeView1.Items.Add(node);

                    node.Directory = drive.RootDirectory.FullName;
                }
            }

            if (!fileView.IsDirectory)
                return;

            if (string.Compare(currentDirectory, fileView.FullName, true) == 0)
                return;

            List<string> nodeNames = new List<string>();
            nodeNames.Add(fileView.FullName);
            string upLevel = Path.GetDirectoryName(fileView.FullName);
            while (!string.IsNullOrEmpty(upLevel))
            {
                nodeNames.Add(upLevel);
                upLevel = Path.GetDirectoryName(upLevel);
            }

            ItemCollection nodes = treeView1.Items;
            TreeViewItem parentNode = null;

            TreeViewBeginUpdate();
            _suppressEventHandling = true;
            try
            {
                for (int i = nodeNames.Count - 1; i >= 0; i--)
                {
                    parentNode = FindNode(parentNode, nodes, nodeNames[i]);
                    nodes = parentNode.Items;
                }

                if (parentNode != null)
                {
                    UpdateNodeChildren((DirectoryTreeNode)parentNode);
                    parentNode.IsSelected = true;
                    parentNode.IsExpanded = true;
                    parentNode.BringIntoView();
                }
            }
            finally
            {
                _suppressEventHandling = false;
                TreeViewEndUpdate();
            }
        }
예제 #2
0
        private void _viewModel_EnhancedPropertyChanged(object sender, EnhancedPropertyChangedEventArgs<IDirectoryController> e)
        {
            if (e.IsProperty(p => p.CurrentDirectory))
            {
                string directory = _viewModel.CurrentDirectory;
                if (Directory.Exists(directory))
                {
                    ButtonGrid.Children.Clear();
                    ButtonGrid.ColumnDefinitions.Clear();

                    List<UIElement> buttons = new List<UIElement>();

                    string curdir = directory;
                    while (Directory.Exists(curdir))
                    {
                        if (curdir.EndsWith(@"\") && curdir.Length > 3)
                            curdir = curdir.Substring(0, curdir.Length - 1);

                        MenuItem menuItem;
                        Menu menu;
                        GetMenuItem(curdir, directory, out menuItem, out menu);

                        if (menu != null)
                        {
                            buttons.Add(menu);
                        }

                        Button dirButton = new Button();
                        dirButton.Content =
                            new TextBlock
                            {
                                Text = curdir.Length <= 3 ? DriveTypeHelper.GetDescription(new DriveInfo(curdir.Substring(0, 2))) : Path.GetFileName(curdir),
                                Margin = new Thickness(2, 0, 2, 0)
                            };

                        dirButton.VerticalAlignment = VerticalAlignment.Stretch;
                        dirButton.VerticalContentAlignment = VerticalAlignment.Center;
                        dirButton.Tag = curdir;
                        dirButton.Click += (s, be) => { _viewModel.CurrentDirectory = (string)((Button)s).Tag; };
                        dirButton.Style = (Style)Application.Current.Resources["DirectoryButton"];
                        buttons.Add(dirButton);

                        if (menuItem != null)
                        {
                            dirButton.MouseEnter += delegate
                            {
                                if (!menuItem.IsSubmenuOpen)
                                {
                                    VisualStateManager.GoToState(menuItem, "MouseOver", true);
                                }
                            };
                            menuItem.MouseEnter += delegate
                            {
                                if (!menuItem.IsSubmenuOpen)
                                {
                                    VisualStateManager.GoToState(dirButton, "MenuMouseOver", true);
                                    VisualStateManager.GoToState(menuItem, "MouseOver", true);
                                }
                            };

                            dirButton.MouseLeave += delegate
                            {
                                if (!menuItem.IsMouseOver && !menuItem.IsSubmenuOpen)
                                {
                                    VisualStateManager.GoToState(menuItem, "Normal", true);
                                }
                            };
                            menuItem.MouseLeave += delegate
                            {
                                if (!dirButton.IsMouseOver && !menuItem.IsSubmenuOpen)
                                {
                                    VisualStateManager.GoToState(dirButton, "Normal", true);
                                    VisualStateManager.GoToState(menuItem, "Normal", true);
                                }
                            };

                            menuItem.SubmenuOpened += delegate
                            {
                                VisualStateManager.GoToState(dirButton, "Pressed", true);
                                VisualStateManager.GoToState(menuItem, "Pressed", true);
                            };

                            menuItem.SubmenuClosed += delegate
                            {
                                if (!dirButton.IsMouseOver && !menuItem.IsMouseOver)
                                {
                                    VisualStateManager.GoToState(dirButton, "Normal", true);
                                    VisualStateManager.GoToState(menuItem, "Normal", true);
                                }
                                else
                                {
                                    VisualStateManager.GoToState(dirButton, "MouseOver", true);
                                    VisualStateManager.GoToState(menuItem, "MouseOver", true);
                                }
                            };
                        }

                        curdir = Path.GetDirectoryName(curdir);
                    }

                    MenuItem rootMenuItem;
                    Menu rootMenu;
                    GetMenuItem(curdir, directory, out rootMenuItem, out rootMenu);
                    rootMenuItem.MouseEnter += delegate
                    {
                        if (!rootMenuItem.IsSubmenuOpen)
                            VisualStateManager.GoToState(rootMenuItem, "MouseOver", true);
                    };

                    rootMenuItem.MouseLeave += delegate
                    {
                        if (!rootMenuItem.IsSubmenuOpen)
                        {
                            VisualStateManager.GoToState(rootMenuItem, "Normal", true);
                        }
                    };

                    rootMenuItem.SubmenuOpened += delegate
                    {
                        VisualStateManager.GoToState(rootMenuItem, "Pressed", true);
                    };

                    rootMenuItem.SubmenuClosed += delegate
                    {
                        if (!rootMenuItem.IsMouseOver)
                        {
                            VisualStateManager.GoToState(rootMenuItem, "Normal", true);
                        }
                        else
                        {
                            VisualStateManager.GoToState(rootMenuItem, "MouseOver", true);
                        }
                    };

                    buttons.Add(rootMenu);

                    FileView currentFileView = new FileView(directory);
                    buttons.Add(new Image { Source = currentFileView.ImageSource, Margin = new Thickness(3, 3, 3, 3), VerticalAlignment = VerticalAlignment.Center });

                    for (int i = buttons.Count - 1, j = 0; i >= 0; i--, j++)
                    {
                        ButtonGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
                        buttons[i].SetValue(Grid.ColumnProperty, j);
                        ButtonGrid.Children.Add(buttons[i]);
                    }
                }
            }
        }