Here's the deal: I have to take a SelectedItem
from a Listbox
, that I got from this question and add it to a ListBox
in another UserControl. The ViewModels and models are all setup, I just need to know how to refer to the ListBox that is getting the items.
This would be under ViewModel A -- the ViewModel that controls the user control with the ListBox that receives the items.
//This is located in ViewModelA
private void buttonClick_Command()
{
//ListBoxA.Items.Add(ViewModelB -> SelectedListItem);
}
I don't understand how to get ListBoxA.
Would it be an ObservableCollection
of strings
?
For further clarification: ListBoxA, controlled by ViewModelA will be receiving values from ListBoxB in ViewModelB. I have included a property for ViewModelB in ViewModelA
You need to have a property in ViewModelA that can be any type that implements IEnumerable. I will use a list:
Then in your Listbox, you need to set the ItemsSource to this list
Now in your constructer, fill MyList with the data you want to display, and on the Add Command, you want to put
ViewModelB.myString assuming from your previous question that in ViewModelB you have a property myString bound to the SelectedItem of ListBoxB, and you have a reference to the instance of ViewModelB in ViewModelA.
This should do it, let me know
Update:
You should be using an ObservableCollection in VMA since the collection will be added to.