Reputation: 819
I have a program that when you click the button, it creates a person with random attributes.
If the content of the label changes with every different object (person) created, how do you define that in true MVVM style? I can't have the viewmodel control the view, right? So i can't
label.Content = person.hair_Color;
public class Person()
get set hair_Color, get set shirt_color, yadda yadda
Because there can be either 1 or an infinite amount of people, how do i dynamically add the content of a label, if i don't know how many there will be?
Upvotes: 0
Views: 1642
Reputation: 3188
In 'true MVVM style', you would have something like:
<Button Command={Binding AddPerson}
/><ListBox ItemsSource="{Binding Persons}"/>
<TextBlock Text="{Binding Shirt}"/>
<TextBlock Text="{Binding Hair}"/>
<Rectangle Background="{Binding Shirt, Converter={stringToColorConverter}/>
<Rectangle Background="{Binding Hair, Converter={stringToColorConverter}/>
public ObservableCollection<PersonViewModel> Persons { get; set; }
public Command AddPerson { get; set; }
public string Shirt { get; set; }
public string Hair { get; set; }
This is pretty much just a mockup of what you would actually have, since implementation depends on the framework used, but the idea is here. You bind, you convert, etc.
Upvotes: 3