j0ey_wh j0ey_wh - 3 months ago 7
C# Question

Using the binded object C#/XAML

I'm new to XAML, and maybe my question is a little bit stupid, but, here is my problem.

I have a

ListView
, and i have set the
ItemSource
to a ObservableList inside a ModelView class

Here's how my code looks like:

public class ListModelView
{
ObservableList<MyClass> _list = new ObservableList<MyClass>();
public ObservableList<MyClass> MyList { get{return _list;} set{}}

public ListModelView()
{
_list = methodThatReturnsAListFromAWebService;
}
}


Every thing works just fine. But how do I modify (add, delete) elements from
MyList
. To get it work i've modified properties as
static
, and it work. But i wonder if this is the right way to do it. Thank You.

Alx Alx
Answer

You can access it by giving your view model in XAML, like this:

<viewModel:YourViewModel x:Name="ViewModelName"/>

Now you'll be able to access it in the code behind. For further reading i'd recommend Messenger from MVVM light toolkit, you can communicate between your View Models using this class

Comments