You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#反射:如何获取并修改Navisworks各Tab的IsSelected属性?

Working with Tab ViewModel Properties via Reflection in Navisworks

Great question! Since you can't reference the required Autodesk DLLs directly, reflection is absolutely the way to go here. Let's walk through how to read the IsSelected property for each of your tabs, and how to modify it (with UI updates included).

Step 1: Get Instances of Each Tab ViewModel

First, you'll need to access the actual instances of TasksTabVM, DataSourcesTabVM, ConfigureTabVM, and SimulateTabVM. These are almost certainly properties of a parent view model (like a main Timeliner VM) bound to the UI. Here's how to grab those instances via reflection:

// First, get the parent view model (source this from Navisworks' UI)
// Example: Access the Timeliner window's DataContext
var timelinerWindow = System.Windows.Application.Current.Windows
    .OfType<System.Windows.Window>()
    .FirstOrDefault(w => w.Title.Contains("Timeliner")); // Adjust title match for your Navisworks version
var parentViewModel = timelinerWindow?.DataContext;

// Grab each tab VM instance
var tasksTabVm = parentViewModel?.GetType()
    .GetProperty("TasksTabVM", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
    ?.GetValue(parentViewModel);

var dataSourcesTabVm = parentViewModel?.GetType()
    .GetProperty("DataSourcesTabVM", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
    ?.GetValue(parentViewModel);

var configureTabVm = parentViewModel?.GetType()
    .GetProperty("ConfigureTabVM", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
    ?.GetValue(parentViewModel);

var simulateTabVm = parentViewModel?.GetType()
    .GetProperty("SimulateTabVM", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
    ?.GetValue(parentViewModel);

Step 2: Read the IsSelected Property Value

All these tab VMs inherit from BaseVM (even if you can't reference it directly), so they'll all expose the IsSelected property. Create a helper method to safely read this value across any tab VM:

bool GetTabIsSelected(object tabViewModel)
{
    if (tabViewModel == null) return false;

    var isSelectedProperty = tabViewModel.GetType()
        .GetProperty("IsSelected", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

    return isSelectedProperty != null ? (bool)isSelectedProperty.GetValue(tabViewModel) : false;
}

// Usage:
bool isTasksTabSelected = GetTabIsSelected(tasksTabVm);
bool isDataSourcesTabSelected = GetTabIsSelected(dataSourcesTabVm);
bool isConfigureTabSelected = GetTabIsSelected(configureTabVm);
bool isSimulateTabSelected = GetTabIsSelected(simulateTabVm);

Step 3: Modify the IsSelected Property (and Update the UI)

Just setting the property value via reflection won't update the UI—you need to trigger the PropertyChanged event (since these VMs implement INotifyPropertyChanged). Here's a helper method that handles both:

void SetTabIsSelected(object tabViewModel, bool isSelected)
{
    if (tabViewModel == null) return;

    var vmType = tabViewModel.GetType();

    // 1. Set the IsSelected property value
    var isSelectedProperty = vmType.GetProperty("IsSelected", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
    isSelectedProperty?.SetValue(tabViewModel, isSelected);

    // 2. Trigger PropertyChanged to update the UI
    var propertyChangedEvent = vmType.GetEvent("PropertyChanged");
    if (propertyChangedEvent != null)
    {
        // Create the PropertyChangedEventArgs for "IsSelected"
        var argsType = System.Type.GetType("System.ComponentModel.PropertyChangedEventArgs");
        var eventArgs = System.Activator.CreateInstance(argsType, "IsSelected");

        // Invoke the event to notify the UI
        propertyChangedEvent.Invoke(tabViewModel, new[] { eventArgs });
    }
}

// Usage: Select the Simulate tab
SetTabIsSelected(simulateTabVm, true);

Key Notes & Troubleshooting

  • Binding Flags: Always specify BindingFlags.Public | BindingFlags.Instance when looking up properties/events—this ensures you're targeting the correct instance members.
  • Parent VM Access: If you can't get the parent VM via the window's DataContext, you might need to use Win32 API to find Navisworks' Timeliner window, or explore other UI elements via reflection to trace back to the VM.
  • Version Compatibility: Navisworks updates might change VM property names or structures, so add null checks and try/catch blocks to handle unexpected changes.
  • Security: Some Navisworks environments might restrict reflection operations—test thoroughly in your target version.

内容的提问来源于stack exchange,提问作者Oosutsuke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:16:14