diff --git a/src/Bonsai.Gui/PropertyGridBuilder.cs b/src/Bonsai.Gui/PropertyGridBuilder.cs index 34bc7dc..cca8e12 100644 --- a/src/Bonsai.Gui/PropertyGridBuilder.cs +++ b/src/Bonsai.Gui/PropertyGridBuilder.cs @@ -13,6 +13,7 @@ public class PropertyGridBuilder : ControlBuilderBase { internal readonly BehaviorSubject _HelpVisible = new(true); internal readonly BehaviorSubject _ToolbarVisible = new(true); + internal readonly BehaviorSubject _RefreshProperties = new(false); /// /// Gets or sets a value specifying whether the help text box is visible. @@ -35,5 +36,17 @@ public bool ToolbarVisible get => _ToolbarVisible.Value; set => _ToolbarVisible.OnNext(value); } + + /// + /// Gets or sets a value specifying whether the property grid should refresh + /// whenever the value of any property changes. + /// + [Category(nameof(CategoryAttribute.Behavior))] + [Description("Specifies whether the property grid should refresh whenever the value of any property changes.")] + public bool RefreshProperties + { + get => _RefreshProperties.Value; + set => _RefreshProperties.OnNext(value); + } } } diff --git a/src/Bonsai.Gui/PropertyGridVisualizer.cs b/src/Bonsai.Gui/PropertyGridVisualizer.cs index 6b2611f..2674d20 100644 --- a/src/Bonsai.Gui/PropertyGridVisualizer.cs +++ b/src/Bonsai.Gui/PropertyGridVisualizer.cs @@ -15,18 +15,42 @@ public class PropertyGridVisualizer : ControlVisualizerBase protected override PropertyGrid CreateControl(IServiceProvider provider, PropertyGridBuilder builder) { + var handlerRegistered = false; var propertyGrid = new PropertyGrid(); + propertyGrid.Tag = builder; propertyGrid.Dock = DockStyle.Fill; propertyGrid.Size = new Size(350, 450); propertyGrid.Site = new ServiceProviderContext(provider); propertyGrid.SubscribeTo(builder._HelpVisible, value => propertyGrid.HelpVisible = value); propertyGrid.SubscribeTo(builder._ToolbarVisible, value => propertyGrid.ToolbarVisible = value); + propertyGrid.SubscribeTo(builder._RefreshProperties, value => + { + if (value) + { + propertyGrid.Refresh(); + if (!handlerRegistered) + { + propertyGrid.PropertyValueChanged += PropertyGrid_PropertyValueChanged; + handlerRegistered = true; + } + } + }); var workflowBuilder = (WorkflowBuilder)provider.GetService(typeof(WorkflowBuilder)); propertyGrid.SelectedObject = GetExpressionContext(workflowBuilder.Workflow, builder); return propertyGrid; } + static void PropertyGrid_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e) + { + var propertyGrid = (PropertyGrid)sender; + var builder = (PropertyGridBuilder)propertyGrid.Tag; + if (builder._RefreshProperties.Value) + { + propertyGrid.Refresh(); + } + } + static ExpressionBuilderGraph GetExpressionContext(ExpressionBuilderGraph source, ExpressionBuilder target) { foreach (var element in source.Elements())