如何在 Revit 功能区面板中堆叠按钮(网格布局)

How to stack buttons (grid layout) in a Revit Ribbon Panel

我创建了一个自定义面板,并将其放置在 Revit Modify 选项卡中。目前,按钮严格水平放置。我想以网格格式(行和列)堆叠它们。有谁知道访问布局属性的方式或任何其他实现此目的的方式?

代码:

var pSource = new Autodesk.Windows.RibbonPanelSource()
{
    Title = sPanel,
    KeyTip = "P",
    Id = "autosprink_shr",
    IsSlideOutPanelVisible = true,
    Description = null,
    DialogLauncher = null,
    UID = null,
    Tag = null,
    Name = null
};

var hPanel = new Autodesk.Windows.RibbonPanel()
{
    Id = sPanel,
    Source = pSource,
    IsVisible = true,
    IsEnabled = true, 
};

string sBtnText;

pIcon = Properties.Resources.AS_Revit_UI_SelectLike_icon.GetHbitmap();
sBtnText = "Select All Like";
var pPanelBtn = CreatePanelButton(pIcon, sBtnText, "id_asselectalllike", "", 32, 32, BUTTONSIZE.LARGE);
Autodesk.Windows.ComponentManager.UIElementActivated += SelectAllLikeBtn;
hPanel.Source.Items.Add(pPanelBtn);

是的。当您调用命令将 RibbonItem 添加到面板时,您应该改为调用 RibbonPanel.AddStackedItems();。您可以传入两个或三个,它会将它们堆叠为一个 "column".

所以,经过一番努力,我解决了我的问题(为 Mike 和 Jeremy 提供了一些有用的建议 ^^)。如果您想将自己的功能区面板添加到像这样的网格布局中的 Revit 修改 选项卡,这有点麻烦,但完全值得!

首先,创建一个函数以将 Panel 添加到选项卡:

public Autodesk.Windows.RibbonPanel CreateCustomPanel()
{
    string sPanel = "Panel Name";
    IntPtr pIcon;

    var pSource = new Autodesk.Windows.RibbonPanelSource()
    {
        Title = sPanel, 
        Id = "id_name", 
        IsSlideOutPanelVisible = true, 
        Description = null,
        DialogLauncher = null, 
        UID = null, 
        Tag = null, 
        Name = null,
    };

    var pRow1Source = new Autodesk.Windows.RibbonSubPanelSource()
    {
        Description = null, Id = "row1_shr", Name = null, Tag = null, UID = null
    };
    var pRow2Source = new Autodesk.Windows.RibbonSubPanelSource()
    {
        Description = null, Id = "row2_shr", Name = null, Tag = null, UID = null
    };

    var hPanel = new Autodesk.Windows.RibbonPanel()
    {
        Source = pSource, IsVisible = true, IsEnabled = true
    };

    var hPanelRow1 = new Autodesk.Windows.RibbonRowPanel()
    {
        Height = 32, Id = "pRow1", IsEnabled = true, IsVisible = true, Source = pRow1Source
    };

    var hPanelRow2 = new Autodesk.Windows.RibbonRowPanel()
    {
        Height = 32, Id = "pRow1", IsEnabled = true, IsVisible = true, Source = pRow2Source
    };

    Autodesk.Windows.RibbonSeparator pBreak = new Autodesk.Windows.RibbonSeparator();
    pBreak.SeparatorStyle = Autodesk.Windows.RibbonSeparatorStyle.Invisible;

    string sBtnText;
    // Add buttons to Row 1
    pIcon = Properties.Resources.YourResourceName.GetHBitmap();
    sBtnText = "Button Text";
    var pPanelBtn = new Autodesk.Windows.RibbonButton()
    {
        //SET BUTTON PROPERTIES HERE
    };
    Autodesk.Windows.ComponentManager.UIElementActivated += BtnEventHandler;
    hPanelRow1.Source.Items.Add(pPanelBtn);
    hPanelRow1.Source.Items.Add(pBreak);

    //Repeat for more buttons
    hPanel.Source.Items.Add(hPanelRow1);
    hPanel.Source.Items.Add(new Autodesk.Windows.RibbonRowBreak());
    //Repeat for second row
    hPanel.Source.Items.Add(hPanelRow2);

    return hPanel;
}

那么,就这么简单:

Autodesk.Windows.RibbonControl pRibbon = Autodesk.Windows.ComponentManager.Ribbon;
foreach (var pTab in pRibbon.Tabs)
{
    if (pTab.Id == "Modify")
    {
        pTab.Panels.Add(CreateCustomPanel());
        break;
    }
}