From 0018fd58099e8153a3dd381fd54bf1d4fc92cf89 Mon Sep 17 00:00:00 2001 From: Trygve Wastvedt Date: Mon, 4 Dec 2023 15:15:01 -0600 Subject: [PATCH] REVIT-215698: Restrict where OnNodeModified is called. (#14656) * Restrict where OnNodeModified is called. * Clean up. * Dispose * No SuppressFinalize (cherry picked from commit f1f9b7436153266539903cecb7d298b6f0eb73dc) --- src/Libraries/CoreNodeModels/DropDown.cs | 9 ++-- .../NodeViewCustomizations/DSDropDownBase.cs | 54 +++++++++++-------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/Libraries/CoreNodeModels/DropDown.cs b/src/Libraries/CoreNodeModels/DropDown.cs index 87fab94c4d2..71ec3ce9758 100644 --- a/src/Libraries/CoreNodeModels/DropDown.cs +++ b/src/Libraries/CoreNodeModels/DropDown.cs @@ -59,7 +59,7 @@ public ObservableCollection Items set { items = value; - RaisePropertyChanged("Items"); + RaisePropertyChanged(nameof(Items)); } } @@ -106,7 +106,6 @@ public int SelectedIndex selectedString = GetSelectedStringFromItem(Items.ElementAt(value)); } - OnNodeModified(); RaisePropertyChanged("SelectedIndex"); RaisePropertyChanged("SelectedString"); } @@ -137,12 +136,12 @@ public string SelectedString if (item != null) { selectedIndex = Items.IndexOf(item); - RaisePropertyChanged("SelectedIndex"); + RaisePropertyChanged(nameof(SelectedIndex)); } } selectedString = value; - RaisePropertyChanged("SelectedString"); + RaisePropertyChanged(nameof(SelectedString)); } } @@ -203,6 +202,8 @@ protected override bool UpdateValueCore(UpdateValueParams updateValueParams) Warning(Dynamo.Properties.Resources.NothingIsSelectedWarning); } + OnNodeModified(); + return true; } diff --git a/src/Libraries/CoreNodeModelsWpf/NodeViewCustomizations/DSDropDownBase.cs b/src/Libraries/CoreNodeModelsWpf/NodeViewCustomizations/DSDropDownBase.cs index c54356fda83..e6456345949 100644 --- a/src/Libraries/CoreNodeModelsWpf/NodeViewCustomizations/DSDropDownBase.cs +++ b/src/Libraries/CoreNodeModelsWpf/NodeViewCustomizations/DSDropDownBase.cs @@ -15,61 +15,69 @@ namespace CoreNodeModelsWpf.Nodes public class DropDownNodeViewCustomization : INodeViewCustomization { private DSDropDownBase model; + private ComboBox comboBox; public void CustomizeView(DSDropDownBase model, NodeView nodeView) { this.model = model; - //add a drop down list to the window - var combo = new ComboBox + // Add a drop down list to the window + comboBox = new ComboBox { - Width = System.Double.NaN, - MinWidth= 150, + Width = double.NaN, + MinWidth = 150, Height = Configurations.PortHeightInPixels, HorizontalAlignment = HorizontalAlignment.Stretch, - VerticalAlignment = VerticalAlignment.Center + VerticalAlignment = VerticalAlignment.Center, + Style = (Style)SharedDictionaryManager.DynamoModernDictionary["RefreshComboBox"] }; + nodeView.inputGrid.Children.Add(comboBox); + Grid.SetColumn(comboBox, 0); + Grid.SetRow(comboBox, 0); - combo.Style = (Style)SharedDictionaryManager.DynamoModernDictionary["RefreshComboBox"]; + comboBox.DropDownOpened += DropDownOpened; + comboBox.SelectionChanged += SelectionChanged; - nodeView.inputGrid.Children.Add(combo); - System.Windows.Controls.Grid.SetColumn(combo, 0); - System.Windows.Controls.Grid.SetRow(combo, 0); + comboBox.DataContext = model; - combo.DropDownOpened += combo_DropDownOpened; - - combo.DataContext = model; - - // bind this combo box to the selected item hash - var bindingVal = new System.Windows.Data.Binding("Items") + // Bind this combo box to the selected item hash. + var bindingVal = new Binding(nameof(DSDropDownBase.Items)) { Mode = BindingMode.TwoWay, Source = model }; - combo.SetBinding(ItemsControl.ItemsSourceProperty, bindingVal); + comboBox.SetBinding(ItemsControl.ItemsSourceProperty, bindingVal); - // bind the selected index to the model property SelectedIndex - var indexBinding = new Binding("SelectedIndex") + // Bind the selected index to the model property SelectedIndex. + var indexBinding = new Binding(nameof(DSDropDownBase.SelectedIndex)) { Mode = BindingMode.TwoWay, Source = model }; - combo.SetBinding(Selector.SelectedIndexProperty, indexBinding); + comboBox.SetBinding(Selector.SelectedIndexProperty, indexBinding); + } + + private void SelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (comboBox.SelectedIndex != -1) + { + model.OnNodeModified(); + } } public void Dispose() { + comboBox.DropDownOpened -= DropDownOpened; + comboBox.SelectionChanged -= SelectionChanged; } /// /// When the dropdown is opened, the node's implementation of PopulateItems is called /// - /// - /// - void combo_DropDownOpened(object sender, EventArgs e) + void DropDownOpened(object sender, EventArgs e) { - this.model.PopulateItems(); + model.PopulateItems(); } }