But to access the
Players property of the ViewModel class, I can only instantiate a
new object and can't access the "original" Players collection (?).
I guess I'm having a huge understanding problem here:
To not leave anything out, this is the xml (unchanged from the tutorial):
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="600">
<Grid.Resources>
<DataModel3 x:Name="dataModel" />
<DataTemplate x:Key="TaskTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Rectangle Width="15" Height="10" Fill="{Binding Color}" Stroke="Transparent" StrokeThickness="0"
Margin="5, 0, 5, 0" Grid.Column="0"/>
<TextBlock Text="{Binding Name}" Margin="5, 0, 5, 0" Grid.Column="1"/>
<TextBlock Text="{Binding Scale, StringFormat=P0}" Margin="5, 0, 5, 0" Grid.Column="2"/>
<TextBlock Text="{Binding Pos}" Margin="5, 0, 5, 0" Grid.Column="3"/>
</Grid>
</DataTemplate>
</Grid.Resources>
<ListBox Height="100" DataContext="{StaticResource dataModel}"
ItemsSource="{Binding Players}"
ItemTemplate="{StaticResource TaskTemplate}" />
</Grid>
The classes are also unchanged:
public class Player
{
public Player(string name, Noesis.Color color, float scale, string pos)
{
Name = name;
Color = new SolidColorBrush(color);
Scale = scale;
Pos = pos;
}
public string Name { get; private set; }
public Brush Color { get; private set; }
public float Scale { get; private set; }
public string Pos { get; private set; }
}
public class DataModel3
{
public DataModel3()
{
Players = new ObservableCollection<Player>();
Players.Add(new Player("Player0", Colors.Red, 1.0f, "(0,0,0)"));
Players.Add(new Player("Player1", Colors.Gray, 0.75f, "(0,30,0)"));
Players.Add(new Player("Player2", Colors.Orange, 0.50f, "(0,-10,0)"));
Players.Add(new Player("Player3", Colors.Green, 0.85f, "(0,-10,0)"));
}
public ObservableCollection<Player> Players { get; private set; }
}
Now what can I do in my code to update the List?
In your example you left out the (for me) important part:
ViewModel vm = ...;
vm.Players.Add(new Player { Name = name });
What do I have to write for "..."?
I tried this:
public void AddPlayer()
{
Debug.Log("AddPlayer button clicked");
DataModel3 dm = new DataModel3();
dm.Players.Add(new Player("Player4", Colors.Yellow, 3.0f, "(5,0,0)"));
}
Nothing happens...