Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Updating View with MVVM

retug_austin
Enthusiast

Updating View with MVVM

retug_austin
Enthusiast
Enthusiast

I made a recent post here about MVVM and keeping a list of selected elements updated with updates in the Revit model.

 

I have made some progress after the guidance provided in that first post, but still running into an issue I can't seem to resolve. After some review, it seemed like IUpdater interface would be the best way to keep my view up to date with the revit model changes. Full code on github.

 

Debugging in the Execute command, the model elements are correctly revised based on updates in performed in the revit model (see if gif below, one of the items changes from a W18x40 to a W21x44, the list of IEnumerable<RevitFramingModel> structuralFramingElements is correctly updated while debugging)

 

Recording 2024-03-31 at 14.43.09.gif

 

The problem, the datagrid view is not updated with this data. 

 

Do you know where my problem is? I can't figure out why the view won't update. 

 

retug_austin_0-1711918258692.png

Thanks!

0 Likes
Reply
Accepted solutions (1)
534 Views
3 Replies
Replies (3)

scgq425
Advocate
Advocate
Accepted solution

Hi @retug_austin :

i download u code and debug this , if u want changed type and trigged the window , need to change the code to this , also can add this code `d:DataContext="{d:DesignInstance Type = local:MainViewModel}"` to u mainwindow xaml  ,  this is easy ,so i not push this code to u github .

 

```

public class MainViewModel : INotifyPropertyChanged{

private ObservableCollection<RevitFramingModel> _structuralFramingElements = new ObservableCollection<RevitFramingModel>();

public ObservableCollection<RevitFramingModel> StructuralFramingElements
{
get { return _structuralFramingElements; }
set
{
_structuralFramingElements = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(StructuralFramingElements)));
}
}

 

}

 

```

LanHui Xu
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

retug_austin
Enthusiast
Enthusiast

Thanks a ton @scgq425 , this worked.

 

May I ask, why did you switch from IEnumerable to ObservableCollection on _stucturalFramingElements and StructuralFramingElements? 

 

So cool seeing these sync'd up:

Recording 2024-04-01 at 07.40.34.gif

 

0 Likes

ricaun
Advisor
Advisor

When working with 'Binding' and a list of objects, it is better to use the 'ObservableCollection'.


The 'ObservableCollection' has the 'INotifyPropertyChanged' and 'INotifyCollectionChanged', if you add or remove an element by default the UI gonna be updated.

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils