public void SetupCommands(DirectoryTree dtree, DirectoryTreeViewModel rootModel) { #region RefreshCommand RefreshCommand = new SimpleRoutedCommand(NavigationCommands.Refresh) { CanExecuteDelegate = x => true, ExecuteDelegate = x => rootModel.SelectedDirectoryModel.Refresh() }; #endregion #region RenamdCommand RenameCommand = new SimpleRoutedCommand(ApplicationCommands.SaveAs) { CanExecuteDelegate = x => { return rootModel.SelectedDirectoryModel != null && rootModel.SelectedDirectoryModel.EmbeddedModel.IsEditable; }, ExecuteDelegate = x => { if (dtree._lastSelectedContainer != null) { DirectoryTree.SetIsEditing(dtree._lastSelectedContainer, true); } } }; #endregion RenamdCommand }
public DirectoryTreeCommands(DirectoryTree dtree, DirectoryTreeViewModel rootModel) { Func<ExModel[]> getSelectionFunc = new Func<ExModel[]>(() => { return new ExModel[] { rootModel.SelectedDirectoryModel.EmbeddedModel }; }); Func<DirectoryModel> getCurrentFunc = new Func<DirectoryModel>(() => { return rootModel.SelectedDirectoryModel.EmbeddedDirectoryModel; }); Func<System.Drawing.Point> getMousePositionFunc = new Func<System.Drawing.Point>(() => { Point pt = dtree.PointToScreen(Mouse.GetPosition(dtree)); return new System.Drawing.Point((int)pt.X, (int)pt.Y); }); SetupCommands(getSelectionFunc, getCurrentFunc, getMousePositionFunc); SetupCommands(dtree, rootModel); SimpleRoutedCommand.Register(typeof(DirectoryTree), RefreshCommand); SimpleRoutedCommand.Register(typeof(DirectoryTree), RenameCommand, new KeyGesture(Key.F2)); SimpleRoutedCommand.Register(typeof(DirectoryTree), ContextMenuCommand); SimpleRoutedCommand.Register(typeof(DirectoryTree), PropertiesCommand); SimpleRoutedCommand.Register(typeof(DirectoryTree), CopyCommand); SimpleRoutedCommand.Register(typeof(DirectoryTree), PasteCommand); SimpleRoutedCommand.Register(typeof(DirectoryTree), DeleteCommand, new KeyGesture(Key.Delete)); dtree.AddHandler(TreeViewItem.MouseRightButtonUpEvent, new MouseButtonEventHandler( (MouseButtonEventHandler)delegate(object sender, MouseButtonEventArgs args) { ApplicationCommands.ContextMenu.Execute(null, dtree); })); }
public DirectoryTree() { InitializeComponent(); if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) return; DataContext = RootModel = new DirectoryTreeViewModel(); RootModel.RootDirectory = DirectoryInfoEx.DesktopDirectory; Commands = new DirectoryTreeCommands(this, RootModel); RootModel.OnProgress += (ProgressEventHandler)delegate(object sender, ProgressEventArgs e) { this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new ThreadStart(delegate { RaiseEvent(new ProgressRoutedEventArgs(ProgressEvent, e)); })); }; W7TreeViewItemUtils.SetIsEnabled(this, true); #region Selection this.AddHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler( (RoutedEventHandler)delegate(object obj, RoutedEventArgs args) { if (SelectedValue is DirectoryTreeItemViewModel) { DirectoryTreeItemViewModel selectedModel = SelectedValue as DirectoryTreeItemViewModel; SelectedDirectory = selectedModel.EmbeddedDirectoryModel.EmbeddedDirectoryEntry; if (args.OriginalSource is TreeViewItem) (args.OriginalSource as TreeViewItem).BringIntoView(); _lastSelectedContainer = (args.OriginalSource as TreeViewItem); } })); //this.SelectedDirectoryPath <---> this.SelectedDirectory Binding selectedDirectoryPathBinding = new Binding("SelectedDirectory"); selectedDirectoryPathBinding.Mode = BindingMode.TwoWay; selectedDirectoryPathBinding.Converter = new ExToStringConverter(); selectedDirectoryPathBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; selectedDirectoryPathBinding.Source = this; this.SetBinding(DirectoryTree.SelectedDirectoryPathProperty, selectedDirectoryPathBinding); //this.SelectedDirectory <---> RootModel.SelectedDirectory Binding selectedDirectoryBinding = new Binding("SelectedDirectory"); selectedDirectoryBinding.Mode = BindingMode.TwoWay; selectedDirectoryBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; selectedDirectoryBinding.Source = RootModel; this.SetBinding(DirectoryTree.SelectedDirectoryProperty, selectedDirectoryBinding); //this.AutoCollapse <---> RootModel.AutoCollapse Binding autoCollapseBinding = new Binding("AutoCollapse"); autoCollapseBinding.Mode = BindingMode.OneWayToSource; autoCollapseBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; autoCollapseBinding.Source = RootModel; this.SetBinding(DirectoryTree.AutoCollapseProperty, autoCollapseBinding); #endregion #region ContextMenuWrapper - Obsoluted //_cmw = new ContextMenuWrapper(); //this.AddHandler(TreeViewItem.MouseRightButtonUpEvent, new MouseButtonEventHandler( // (MouseButtonEventHandler)delegate(object sender, MouseButtonEventArgs args) // { // if (SelectedValue is DirectoryTreeItemViewModel) // { // DirectoryTreeItemViewModel selectedModel = SelectedValue as DirectoryTreeItemViewModel; // Point pt = this.PointToScreen(args.GetPosition(this)); // string command = _cmw.Popup(new FileSystemInfoEx[] { selectedModel.EmbeddedDirectoryModel.EmbeddedDirectoryEntry }, // new System.Drawing.Point((int)pt.X, (int)pt.Y)); // switch (command) // { // case "rename": // if (this.SelectedValue != null) // { // if (_lastSelectedContainer != null) // { // SetIsEditing(_lastSelectedContainer, true); // } // } // break; // case "refresh": // if (this.SelectedValue is DirectoryTreeItemViewModel) // { // (this.SelectedValue as DirectoryTreeItemViewModel).Refresh(); // } // break; // } // } // })); #endregion }