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

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));
}
}
}
view raw ViewmodeBase.cs hosted with ❤ by GitHub

Command

[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;
}
}
view raw dockablecmd.cs hosted with ❤ by GitHub

ViewModel

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

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;
}
}
view raw dockableapp.cs hosted with ❤ by GitHub

Tải xuống : Dockable-RevitAPI-Sample