Mở đầu
Mục đích bài viết này viết ra giúp bạn tạo nhanh một mẫu dock nằm kế bên tương tự với các tab như Project Browser mà bạn thường thấy mỗi ngày
Hình ảnh xem trước
ViewmodeBase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using System.Runtime.CompilerServices; | |
namespace DockableRevitAPI.Controls | |
{ | |
public class ViewModelBase : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "") | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} | |
} |
Command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Transaction(TransactionMode.Manual)] | |
[Regeneration(RegenerationOption.Manual)] | |
class Command : IExternalCommand | |
{ | |
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) | |
{ | |
UIApplication uiapp = commandData.Application; | |
if (DockablePane.PaneIsRegistered(App.PaneId)) | |
{ | |
DockablePane docpanel = uiapp.GetDockablePane(App.PaneId); | |
if (docpanel.IsShown()) | |
docpanel.Hide(); | |
else | |
docpanel.Show(); | |
} | |
else | |
{ | |
return Result.Failed; | |
} | |
return Result.Succeeded; | |
} | |
} |
ViewModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DockableViewModel : ViewModelBase | |
{ | |
public DockableViewModel(UIControlledApplication a) | |
{ | |
a.ViewActivated += OnViewActivated; | |
} | |
private void OnViewActivated(object sender, ViewActivatedEventArgs e) | |
{ | |
this.Document = e.Document; | |
} | |
private Document document; | |
public Document Document | |
{ | |
get => document; | |
set | |
{ | |
document = value; | |
OnPropertyChanged("Document"); | |
} | |
} | |
} |
App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App : IExternalApplication | |
{ | |
public static DocPanel DockPanelProvider; | |
public static DockablePaneId PaneId => new DockablePaneId(new Guid("D12C5388-69C4-4A27-B440-5AF7AF03D5F1")); | |
public static string PaneName => "DocPanel"; | |
public Result OnStartup(UIControlledApplication app) | |
{ | |
DockPanelProvider = new DocPanel() {DataContext = new DockableViewModel(app)}; | |
if (!DockablePane.PaneIsRegistered(PaneId)) | |
{ | |
app.RegisterDockablePane(PaneId,PaneName, DockPanelProvider); | |
} | |
return Result.Succeeded; | |
} | |
public Result OnShutdown(UIControlledApplication a) | |
{ | |
return Result.Succeeded; | |
} | |
} |
Tải xuống : Dockable-RevitAPI-Sample