Reputation: 2439
Following a few examples I coded my first MVVM´s demo. After binding and rendering the basic ones I am trying to bind a list of object to a listview, I´m doing something wrong but I can´t get it.
If someone could give me any clue, I´ll be grateful.
The idea is being this one:
-- VIEWMODEL --
public class IntervectionViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaiseOnPropertyChange([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public IntervectionViewModel()
{
intervencion = new IntervectionModel();
Intervencion1 = intervencion.Intervencion;
}
private List<Intervenciones> _intervencion1;
public List<Intervenciones> Intervencion1
{
get { return _intervencion1; }
set { _intervencion1 = value; RaiseOnPropertyChange(); }
}
}
-- MODEL --
public class IntervectionModel
{
public List<Intervenciones> Intervencion;
public IntervectionModel()
{
for (int i = 0; i < 4; i++)
{
//Class Intervenciones contains attribute
Intervenciones inter = new Intervenciones(i);
this.Intervencion.Add(inter);
}
}
}
public class Intervenciones
{
public string Nombre;
public Intervenciones(int num)
{
this.Jefe = "JEFE" + num + " = JEFE1";
this.Nombre = "NOMBRE" + num + " = NOMBRE1";
this.Timer = "1:23:23";
this.Estado = Enums.Estado.Bloqueado;
this.Interv = new List<Tarea>();
for (int i = 0; i < 8; i++)
{
Tarea t = new Tarea
{
Titulo = this.Nombre + "%" + "TITULO = " + i,
Inicio = DateTime.Now.AddMinutes(i),
Duracion = i * 60,
Encargado = "ENCARGADO = " + i,
Estado = Enums.Estado.Activo,
Miembros = new List<string> { "Miembro" + num + " = " + i, "Miembro1" + num + " = " + i, "Miembro2" + num + " = " + i },
TiempoEjecucion = i + 15
};
Interv.Add(t);
}
}
}
}
-- XAML.CS --
public partial class Page1 : ContentPage
{
public Page1()
{
var p = new IntervectionViewModel();
BindingContext = p;
InitializeComponent();
}
}
-- XAML --
<ListView x:Name="sa" ItemsSource="{ Binding Intervencion1 }" VerticalOptions="Center" HorizontalOptions="Center">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{ Binding attribute }" TextColor="Black" BackgroundColor="Blue"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
XAML is painting 4 blue row (the list binded has 4 element), but the labels haven´t anything inside.
Thanks mates.
Upvotes: 0
Views: 128
Reputation: 2581
Besides that you shouldn't instantiate your ViewModel in the code behind from your View.
Try to set your ViewModel to the DataContext of the View, not the BindingContext. And it seems that you are not implementing the Property "attribute" in your Model.
Upvotes: 0
Reputation: 1741
Class Intervenciones needs a string property "attribute" like that
public string attribute { get; set; }
For your info:
It has to be a property! Not a field. And actually the convention is:
public string Attribute { get; set; }
as it is a public property.
Upvotes: 2