MVVM pattern how can I get a collection of selected ViewPlan objects from a Listbox?

MVVM pattern how can I get a collection of selected ViewPlan objects from a Listbox?

lariasGHNFM
Contributor Contributor
770 Views
8 Replies
Message 1 of 9

MVVM pattern how can I get a collection of selected ViewPlan objects from a Listbox?

lariasGHNFM
Contributor
Contributor

 

How can I get access to the selected ViewPlan objects and store them in a collection? I need a collection of selected items so that I can use the collection to Duplicate the selected floor plans. I know the SelectedItems Property of Listbox is read only so I would like another solution.

0 Likes
771 Views
8 Replies
Replies (8)
Message 2 of 9

RPTHOMAS108
Mentor
Mentor

You can't hold onto such API objects in your view model, your best option is to create your own versions that represent shallow copies. In the end what do you need to see in the UI to select an item (the name), how do you relate that to the actual view plan in the model, via UniqueId etc. So you need an object that holds the UniqueId and the ViewName.

 

Depending on the situation you also have to link the individual object to the particular model (document) it came from or re-create the view model when the model (document) is changed. The VM also has to update the view when the objects are deleted from Revit.

 

So in the MVVM pattern

The VM makes a request of Revit for the view plans

Revit returns view plans which VM converts into your object of Name, UniqueId

VM updates the view with those objects which could be implementing interfaces that represent contract of Name, UniqueId.

 

There is only really a benefit of this abstraction if your view is likely to be different things i.e. access from different places.

 

When people bring these patterns up (MVVM, MVC...) I'm never really sure they fully fit working with the Revit API. Those patterns are usually based on the model being one thing such as a products database. There is also the implication that access to such is continuous (although you could argue the same via IExternalEventHandler is possible with Revit).

Message 3 of 9

lariasGHNFM
Contributor
Contributor

I see what you are saying, I was wondering if MVVM is a good approach for this addin.

do you think I should collect the ViewPlan names, store them in a List of strings and have those strings be the item source for my Listbox?  This would be written in code behind not implementing MVVM. 

0 Likes
Message 4 of 9

RPTHOMAS108
Mentor
Mentor

I wouldn't use strings because then you are relying on Names of things (which are not always unique). You need something more which truly links the name to what it is back in the model.

 

You can bind to a collection of any type of object and as long as you override ToString it will display that in the control for the object.

0 Likes
Message 5 of 9

lariasGHNFM
Contributor
Contributor

ok, I am a just a beginner so what I am understanding is this :

Collect the ViewPlans from the Revit document  and take the name property, use that to display in the UI and take the unique Id or element Id of each ViewPlan and store that in a Collection or list so that I can use it in my Transaction.. let me know if this sounds correct. 

0 Likes
Message 6 of 9

RPTHOMAS108
Mentor
Mentor

1) Collect the ViewPlans from Revit

2) Convert them to your shallow copy of a class which has 'Name' and 'UniqueId' (Override ToString on your class to return 'Name')

3) Put (2) into a collection and bind listbox to it

 

When the listbox selection is changed or if you press another button (whatever the event is) cast the selected item in the listbox to a version of your class from (2).

 

Use the 'UniqueId' of the selected item to re-find that in the document.

 

If your interaction with Revit is modal then you can use ElementId instead of UniqueId. For modeless interaction you also need to ensure that your collections stays in-sync with the items of the active document.

Message 7 of 9

lariasGHNFM
Contributor
Contributor

would this be the correct way to set up my Shell class of ViewPlan?

 

 

public class MyViewPlan
{
private string name { get; set; }

public string Name { get { return name; } set {name = value; } }


private string elementId { get; set; }

public string ElementId { get { return elementId; } set {elementId = value; } }

 

public MyViewPlan(string name, string elementId)
{
Name = name;
ElementId = elementId;
}
}

0 Likes
Message 8 of 9

RPTHOMAS108
Mentor
Mentor

Seems OK just need to override ToString so that the listbox uses the value it returns i.e. 'Name' to display what it is.

0 Likes
Message 9 of 9

lariasGHNFM
Contributor
Contributor

This worked thank you so much!

0 Likes