130 lines
4.1 KiB
C#
130 lines
4.1 KiB
C#
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Media;
|
||
|
||
using CommunityToolkit.Mvvm.ComponentModel;
|
||
|
||
using UIFramework;
|
||
|
||
namespace Sai.Toolkit.Revit.Helpers;
|
||
|
||
#if REVIT2020||REVIT2025
|
||
public static class OptionsBarAssist
|
||
{
|
||
private static readonly Grid RootGrid;
|
||
private static ContentPresenter _panelPresenter;
|
||
private static readonly FrameworkElement InternalToolPanel;
|
||
|
||
static OptionsBarAssist()
|
||
{
|
||
//找到整个Revit界面的Grid
|
||
RootGrid = VisualAssist.FindVisualParent<Grid>(Autodesk.Windows.ComponentManager.Ribbon, "rootGrid");
|
||
if (RootGrid is null)
|
||
{
|
||
throw new InvalidOperationException("无法在 Revit UI 中找到root Grid");
|
||
}
|
||
//选项栏所在的Panel,在第二行Index为1,类型是DialogBarControl
|
||
InternalToolPanel = VisualAssist.FindVisualChild<DialogBarControl>(RootGrid, string.Empty);
|
||
if (InternalToolPanel is null)
|
||
{
|
||
throw new InvalidOperationException("无法在 Revit UI 中找到内部工具面板");
|
||
}
|
||
}
|
||
|
||
public static void ShowOptionsBar(FrameworkElement content)
|
||
{
|
||
if (_panelPresenter is not null)
|
||
{
|
||
//var actuallWidth = 0.0;
|
||
//var controlHost = VisualAssist.FindVisualChild<ControlHost>(InternalToolPanel, string.Empty);
|
||
//var size = VisualTreeHelper.GetChildrenCount(controlHost);
|
||
//for (int i = 0; i < size; i++)
|
||
//{
|
||
// var child = VisualTreeHelper.GetChild(controlHost, 0) as FrameworkElement;
|
||
// actuallWidth += child.ActualWidth;
|
||
//}
|
||
_panelPresenter.Content = content;
|
||
_panelPresenter.Visibility = System.Windows.Visibility.Visible;
|
||
//设为0,隐藏内部的Panel
|
||
//InternalToolPanel.Height = 0;
|
||
return;
|
||
}
|
||
//第一次执行时,会创建
|
||
_panelPresenter = CreateOptionsBar();
|
||
_panelPresenter.Content = content;
|
||
|
||
//InternalToolPanel.Height = 0;
|
||
}
|
||
|
||
public static void HideOptionsBar()
|
||
{
|
||
if (_panelPresenter is null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_panelPresenter.Content = null;
|
||
_panelPresenter.Visibility = System.Windows.Visibility.Collapsed;
|
||
|
||
InternalToolPanel.Height = 26;
|
||
}
|
||
|
||
public static async void HideOptionsBar(TimeSpan delay)
|
||
{
|
||
await Task.Delay(delay);
|
||
HideOptionsBar();
|
||
}
|
||
|
||
private static ContentPresenter CreateOptionsBar()
|
||
{
|
||
const int panelRow = 1;
|
||
//在内部OptionsBar下方插入一行
|
||
RootGrid.RowDefinitions.Insert(panelRow, new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
|
||
//RootGrid.ColumnDefinitions.Insert(1,new ColumnDefinition { Width=new GridLength(1, GridUnitType.Auto) });
|
||
//把下方的所有元素都往下移动一行
|
||
foreach (UIElement child in RootGrid.Children)
|
||
{
|
||
var row = Grid.GetRow(child);
|
||
if (row > 0)
|
||
{
|
||
Grid.SetRow(child, row + 1);
|
||
}
|
||
//Grid.SetColumnSpan(child, 2);
|
||
}
|
||
var panelPresenter = new ContentPresenter();
|
||
Grid.SetRow(panelPresenter, panelRow);
|
||
//Grid.SetColumn(panelPresenter, panelRow);
|
||
RootGrid.Children.Add(panelPresenter);
|
||
|
||
return panelPresenter;
|
||
|
||
//var panelPresenter = new ContentPresenter();
|
||
//Grid.SetRow(panelPresenter, 1);
|
||
//RootGrid.Children.Add(panelPresenter);
|
||
//return panelPresenter;
|
||
}
|
||
/// <summary>
|
||
/// 简单注册并调用
|
||
/// </summary>
|
||
/// <typeparam name="TV"></typeparam>
|
||
/// <typeparam name="TM"></typeparam>
|
||
/// <param name="action"></param>
|
||
public static void RegisterOptionsBar<TV, TM>(Action<TV, TM> action)
|
||
where TV : Panel, new()
|
||
where TM : ObservableObject, new()
|
||
{
|
||
try
|
||
{
|
||
TM viewModel = new();
|
||
var view = new TV() { DataContext = viewModel };
|
||
action(view, viewModel);
|
||
ShowOptionsBar(view);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
#endif
|